Android Question Need help with BlueTooth

MrKim

Well-Known Member
Licensed User
Longtime User
Mea Culpa: I know nothing about blue tooth at this point.

We have a SPC (quality control) application. The users have been typing in the measurements they take. Now they want to use Blue Tooth enabled measuring tools - I have a lot to learn.
I downloaded BLE.b4a and with some minor quick and dirty mods I was able to "see" my blue tooth device.

The log looks like this :

B4X:
Found: SY295, E2:32:C3:6B:40:C7, RSSI = -71, (MyMap) {1=[B@77f17, 9=[B@eb44f04, 0=[B@3f10ced}
Discovering services.
Connected
(Intent) Intent { act=android.bluetooth.device.action.BOND_STATE_CHANGED flg=0x10 (has extras) }
Bundle[{android.bluetooth.device.extra.PREVIOUS_BOND_STATE=10, android.bluetooth.device.extra.DEVICE=E2:32:C3:6B:40:C7, android.bluetooth.device.extra.BOND_STATE=11}]
(Intent) Intent { act=android.bluetooth.device.action.BOND_STATE_CHANGED flg=0x10 (has extras) }
Bundle[{android.bluetooth.device.extra.PREVIOUS_BOND_STATE=11, android.bluetooth.device.extra.DEVICE=E2:32:C3:6B:40:C7, android.bluetooth.device.extra.BOND_STATE=10, android.bluetooth.device.extra.REASON=9}]
Disconnected

I have NO IDEA what to do next. Any help appreciated.

In case your interested it is a Fowler Bluetooth digital caliper.
 

MrKim

Well-Known Member
Licensed User
Longtime User
Note the BLE and classic Bluetooth are completely different things.

Start with running the BLE example. Do you see the list of services and characteristics?
As mentioned, the log entries above are from the BLE.b4a example.
I modified it as follows:
B4X:
   Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
    Log("Found: " & Name & ", " & Id & ", RSSI = " & RSSI & ", " & AdvertisingData)
    If Name = "SY295" Then
        'Return
        ConnectedName = Name
        manager.StopScan
        manager.Connect(Id)
    End If
  
End Sub
So it would only connect to the device I was after (I was working at the clients and about 10 devices showed up willing to connect).

The tool manufacturer simply referred me to this page:

https://developer.android.com/guide/topics/connectivity/bluetooth-le.html#setup

to write my own app.

I am able to pair the device with my tablet. Ideally what I am after is to create a list of paired devices so the user can choose the desired one, and then listen for the data from that device.
 
Last edited:
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User

I am a little slow, but gradually gaining an understanding of what needs to be done. Just not sure how to do it.
I am looking to display a list of already paired devices not devices that are broadcasting.
When one is selected then I need to start listening for data from that device.
The device has 3 modes.
1. It continuously broadcasts data.
2. Broadcast data when values change.
3. Broadcast data when you push the button on the device. or request the data from the device.
I am really interested in 3 and. for now, only when the button is pushed on the device.
The problem I am currently experiencing is (using BLE.b4a) I can connect OK and read data, but as soon as I have read the data I am disconnected from the device. How do I stay connected and continue listening for new data to be broadcast?

Ok, i have done more experimenting. I can get the paired BT devices using Serial (why it is called serial is beyond me) this gives me the MAC Address (called DeviceID in .Connect?).
But I cannot connect to the device unless I scan for it first. I get error:
java.lang.RuntimeException: MacAddress not found. Make sure to call Scan before trying to connect.

BUT it won't find the device using scan if it is not broadcasting. So how do I connect to an already paired device without putting the device in broadcast mode?
And then listen for it to send information.

Thanks for your help.
 
Last edited:
Upvote 0
Top