BT example code

rfsingh81

Member
Licensed User
Longtime User
Hi, I am playing with the BT example. I created an app based on that and it works fine on my tab. When I submitted it to samsung app store they rejected it as it gave some error (screenshot attached). Can someone please explain me about the error and how to rectify it.

Here is goes:
You click on search button. It gives list of devices to connect. When clicked on any device (galaxy 4 tablet in samsung's own app checking stage during the approval process) it gave the mentioned error after the normal message of 'trying to connect......."

Thanks
 

Attachments

  • Function eror.jpg
    Function eror.jpg
    90.3 KB · Views: 239

Beja

Expert
Licensed User
Longtime User
(may be) you need to make sure the bt is open in your phone before running your app.
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Hi rfsingh81,
In this case people will need to look into your code, because according to the error message, the discovery part of your code is not starting.
I am still studying serial communication in b4a, but if you posted the code then someone will help you.
 
Upvote 0

rfsingh81

Member
Licensed User
Longtime User
Code

B4X:
'Activity module
Sub Process_Globals
   Dim admin As BluetoothAdmin
   Dim serial1 As Serial
   Dim foundDevices As List
   Type NameAndMac (Name As String, Mac As String)
   Dim connectedDevice As NameAndMac
End Sub   

Sub Globals
   Dim btnSearchForDevices As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      admin.Initialize("admin")
      serial1.Initialize("serial1")
   End If
   Activity.LoadLayout("1")
   Activity.Title="BlueToothSwitch"
End Sub

Sub Activity_Resume
   btnSearchForDevices.Enabled = False
   If admin.IsEnabled = False Then
      If admin.Enable = False Then 
         ToastMessageShow("Error enabling Bluetooth adapter.", True)
      Else
         ToastMessageShow("Enabling Bluetooth adapter...", False)
         'the StateChanged event will be soon raised
      End If
   Else
      admin_StateChanged(admin.STATE_ON, 0)
   End If
End Sub

Sub admin_StateChanged (NewState As Int, OldState As Int)
   btnSearchForDevices.Enabled = (NewState = admin.STATE_ON)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   If UserClosed = True Then
      serial1.Disconnect
   End If
End Sub

Sub btnSearchForDevices_Click
   foundDevices.Initialize
   If admin.StartDiscovery   = False Then 
      ToastMessageShow("Error starting discovery process.", True)
   Else
      ProgressDialogShow("Searching for devices...")
   End If
End Sub

Sub admin_DiscoveryFinished
   ProgressDialogHide
   If foundDevices.Size = 0 Then
      ToastMessageShow("No device found.", True)
   Else
      Dim l As List
      l.Initialize
      For i = 0 To foundDevices.Size - 1
         Dim nm As NameAndMac
         nm = foundDevices.Get(i)
         l.Add(nm.Name)
      Next
      Dim res As Int
      res = InputList(l, "Choose device to connect", -1)
      If res <> DialogResponse.CANCEL Then
         connectedDevice = foundDevices.Get(res)
         ProgressDialogShow("Trying to connect to: " & connectedDevice.Name & " (" & connectedDevice.Mac & ")")
         serial1.Connect(connectedDevice.Mac)
      End If
   End If
End Sub

Sub admin_DeviceFound (Name As String, MacAddress As String)
   Log(Name & ":" & MacAddress)
   Dim nm As NameAndMac
   nm.Name = Name
   nm.Mac = MacAddress
   foundDevices.Add(nm)
   ProgressDialogShow("Searching for devices (~ device found)...".Replace("~", foundDevices.Size))
End Sub


Sub Serial1_Connected (Success As Boolean)
   Log("Serial Connected")
   ProgressDialogHide
   Log("connected: " & Success)
   If Success = False Then
      Log(LastException.Message)
      ToastMessageShow("Error connecting: " & LastException.Message, True)
   Else
      StartActivity(ChatActivity)
   End If
End Sub

I will say again that discovery part works fine. The list is generated of the devices - it is only after clicking on a device to connect - the error comes after few seconds.
 
Upvote 0

rfsingh81

Member
Licensed User
Longtime User
In the video which they sent me (I tried uploading it but it is too big) they tried with Galaxy S4. I tried uploading my apk here as well so you guys can check it but it says it is big as well.
 
Upvote 0

rfsingh81

Member
Licensed User
Longtime User
Did they try to connect two Galaxy S4 phones?

This error usually means that the other device isn't listening for connections.

No Erel. In the video, they started one device which is listening for connections, then they searched for devices using my app and clicked on that device to connect. It then gave this error. This happened to them with more devices as well.
 
Upvote 0

merlin2049er

Well-Known Member
Licensed User
Longtime User
Cool, this should be helpful. I want to control a bluetooth device, and send it a few instructions to switch things on / off.
 
Upvote 0

rfsingh81

Member
Licensed User
Longtime User
Clarification

You can only connect to a device that is actively listening for connections (on the correct UUID). You need to run an app on the S4 that calls Serial.Listen.

Thanks Erel. I did not understood what is UUID?
I tried to connect the app with the bluetooth module I have, no problems. In fact I tried it with more modules of the same kind, it worked fine as well.

Does it mean it cannot connect to other devices like phones and tablets not running the same chat app? But with BT modules connected to MCU works fine.
 
Upvote 0
Top