I'm trying to implement a BLE connection between two devices:
A Linux app, as the server, that must advertise services and characteristics
An Android smartphone, as the client, that must connect to the server and read characteristics
The issue comes from my Linux app. When I connect my smartphone to my laptop using my android app and bluetoothctl, it works like a charm. However, when I do the same with my Linux app instead of bluetoothctl, it doesn't works (it actually does, but very rarely).
As my Linux app is written in Go, I use go-ble as the bluetooth package. I have the same issue with their minimal code sample, this is the reason I don't write more code here, but I can't figure out why.
I think I just don't know how to use their library correctly, but the documentation isn't very explicit, so if anyone could help...
EDIT
On my smartphone, when using my apps, I see the laptop when scanning, so The HCI device is up. But when attempting to connect with connectGatt(), I get a 133 status code, which is GATT_ERROR.
One probable issue, is I'm not closing the connection, see this thread, but then how could it works with bluetoothctl?
Bluetoothctl is a bluetooth client working either with BLE and BR\EDR bluetooth. It was working because my android app was connecting through "Classic Bluetooth" with gattConnect.
I solved it by specifying the transport protocol to use, replacing:
device.connectGatt(context, false, gattCallback)
with:
device.connectGatt(context, false, gattCallback, BluetoothDevice.TRANSPORT_LE)
Related
I need to estabilish a brief communication between a BLE master and a BLE peripherical device.
Until I have to use the mobile phone I have no problems, since I select the bluetooth device from Android, I can connect to it and then I can receive data simply writing them to HM-10.
BUT, this approach is battery expensive, since the BLE scanning is more battery expensive than the BLE advertising (right?). So I want that the mobile becames an advertisor and the HM-10 takes the ROLE of Slave.
BUT, I still don't understand if it is possible to connect to other devices: I tried AT+CONN and it always fails (AT+CONF). Is there any way to send brief data from the HM-10 to the advertisor, even without connecting?
Even my firmware looks slightly different from the readme! For example,AT+LN is present in the documentation but it appears to not exist. Does a better version of the firmware for this device exist, or, are sources available somewhere? I installed the HM-Soft V705 (lastest).
Thanks
UPDATE 1:
#ChrisStratton Ok, I have some intresting news: I was already following your idea (phone advertise sometimes, while HM-10 always scan and tries to connect to the service if it sees it, then HM-10 send some data, phone answer). I managed to make the phone visible from HM-10 (don't ask me how!). I am using the BLE Tool Android app to test everything. Now, the central problem now is that HM-10 won't connect to the android service even if the advertisor is connectable (see attached images).
I am sospicious that the AT+CO (that I discovered is a kind a replacement of AT+LN, but I am not sure) can help, but i don't know how.
I'm today in front of a problem with bluetooth and web app.
Introduction
First, here is my goal:
I want to connect a KDC (barcode scanner) in a web application (which will be probably write in C#, ASP.Net Core and JS). You can find the model and characteristic of this scanner here : koamtac.com/wp-content/uploads/KDC300.pdf .
The goal of this project is to use this website mostly on tablet and mobile, and not to use scanner with windows (but we would like to be able to see the website in computer aswell)
Sadly, Koamtac gives a SDK for windows local applications, for Android applications, but not really for web applications...
BLE tool
With this objective in mind, i discovered BLE technologies and the new Web Bluetooth API powered by google : Google Web Bluetooth API Implementations Status.
As you can see, we could use it with windows to discover devices but not to be connect with them. (But with android 6+, it is possible to do both).
This tool seems to be just in line with my project but currently i didn't knew really if my KDC could be use as a BLE device. I decided to make a simple web application where i could test if i saw (or not) the KDC device.
Application
navigator.bluetooth.requestDevice(
{
acceptAllDevices: true,
optionalServices: ['battery_service']
})
.then(device => {
console.log('> Found ' + device.name);
console.log('Connecting to GATT Server...');
return device.gatt.connect();
})
I did it on a github rep to get the HTTPS protocol which is needed to empowered the Web Bluetooth API. You can see this app here : My GitHub Repository
Finally, i tried this application with 3 differents states:
With Windows 10, in local (using Chrome Dev Tool)
With Windows, connected to rlamotte.github.io
With Android 6+ on my smartphone, connected to rlamotte.github.io
Before giving my results, you can see every bluetooth device in range of windows/android on the picture named "Device_In_Range_......" in the Github Rep
Results
I had those results:
In local, KDC device is every time found (see Request_Device_Result_Windows_Local). I can't connect to it (see KDC_connect_Windows) because Windows is not able to connect to GATT.
In this page (see Request_Device_Result_Windows), KDC is found 10% of time, and it takes much time to get it (approx 1 min). As you can see, Request found Gwladys, and 81PRMX1.
(see Request_Device_Result_Android), KDC is never found, and the request found EST (a beacon) and JBL Flip 3 (a bluetooth speaker).
Conclusion:
As you can see, the result of request_device is not the same with windows 10 and android 6. But when we use the bluetooth manager of those device, we saw exactly the same devices (the sum of devices found by each request_device).
I'm new to Bluetooth API area and i don't know why Android doesn't show all bluetooth device with request_device.
THANKS for you help ;)
EDIT #1
It seems that KDC 300 doesn't support BLE. I'm currently searching a way to connect bluetooth classic device to web app. If anyone has an idea you can tell me. If i don't find any powerful way to solve it, i'm gonna try to make an hybrid app android/windows with xamarin to use native bluetooth connection.
I'm trying to demo android side service (content provider in ANCS lingo), that communicates with embedded device that supports Apple Notification Service (ANCS) notification consumer. Reason for this is, that the embedded device only supports ANCS. As the ANCS BLE side things are publiced by Apple, this should be possible and I've already added that service + associated characterics to android side gatt server. Embedded device is nordic nRF52 and SW is nordic ANCS demo. Demo works OK with iPhone and apparently with the desktop version of nRF control panel (if added with nordic supplied config and if you have windows machine, which I don't).
However I cannot seem to be able to bond the device with android, that's required before connection. The advertisement is in limited discovery mode (before bonding) so I cannot find it via regular scanning from settings. Also bonding from java code doesn't work. With mBluetoothDevice.createBond() I get ACTION_BOND_STATE_CHANGED callback with state 10 (BluetoothDevice.BOND_NONE) with old state 11(BluetoothDevice.BOND_BONDING). Apparently I would need to exchange keys here, but no idea how to do that.
Also dunno if anyone has done this same exercise before, but any other tips would also be appreciated! Thanks!
Problem was that the phone BLE stack was corrupted or in some invalid state. REboot and removing battery solved the issue with the bonding.
I'm attempting to adapt the BluetoothChat example that comes with the SDK to a single-server, multiple-client configuration. I'm having issues and hope someone can help.
I've created two distinct apps, a server and a client, rather than have a single app function as both server and client, as in the example. So the upon starting the app, the server creates a bluetooth service, which creates a new AcceptThread, which listens for incoming connections. I made a couple of modifications to the example code so that the AcceptThread continues to listen, even after the first connection is made (the example stops listening after the one connection is made, because it only needs to establish connections between two devices). I created an ArrayList to handle the multiple connections. Each time a new connection is attempted from a remote device, a new ConnectedThread is created and added to the list.
The first client connects just fine.
When the second client attempts to connect, the connection fails. The log output is:
D/BluetoothEventLoop( 140): Device property changed: <device address> property: Connected value: true
I/BluetoothService( 140): ACL connected, mAclLinkCount = 2
D/BluetoothEventLoop( 140): Device property changed: <device address> property: Connected value: false
I/BluetoothService( 140): ACL disconnected, mAclLinkCount = 1
So it looks like the second connection attempt is successful, but then it is immediately disconnected. This looks like it's happening at a low level, outside anything the code is handling. This happens with a number of different types of test devices, so I don't think it's due to a particular firmware or hardware issue.
Can anyone help with this?
Also, can anyone point to an example of a server->multiple client connection example using bluetooth with Android? I came across an Air Hockey example:
http://code.google.com/p/apps-for-android/source/browse/#svn%2Ftrunk%2FBTClickLinkCompete%2Fsrc%2Fnet%2Fclc%2Fbt
But this project is very glitchy on my test devices and the actual game won't display properly. Does anyone know if there is an example of BluetoothChat extended to handle multiple connections?
UPDATE:
In attempting to solve this issue, I believe I have successfully extended the official Android Bluetooth Chat example to work with a server connecting to multiple clients.
I've uploaded my modifications to github here.
One device acts as the server by selecting "Menu>Make discoverable". Each client then connects by selecting "Menu>Connect a device". I was successfully able to connect my Motorola Droid and HTC Eris as clients to my Samsung Galaxy Tab 10.1". Each message sent from a client displays only on the server. A message sent from the server displays on all clients.
This was a tedious process getting it to work at all. Connectivity with the HTC devices is sporadic, though. I've read elsewhere that bluetooth on HTC is buggy, so I'm chalking it up to that.
Hopefully someone else will find this example useful. If you have any questions, suggestions, or other feedback, please let me know.
I tested your code. As, you said connecting HTC as master was a problem. I made Samsung S3 as master and HTC as client. it worked. But i tried again with a Sony Ericsson phone. No other device could successfully act as master.
S3 has Blutooth version 4.0 other have earlier version. Do you think the problem is because of Bluetooth stack?
I need to use this concept in a project where the devices will be custom made. Trying to find out weather the problem is related to vendors ( HTC, Sony) or its related to bluetooth stack. and i don't know how to find out. This is not documented. :(
Has anyone faced this?
I have a Bluetooth service that I programmed for .Net on a Windows machine and I would like my Android 2.1 phone to connect to it. The server is listening for the same UUID which the Android is using to connect. But the connection is failing.
When I try to connect to devices that are not listening for that UUID, I get an exception with the message "Service discovery failed", but when I try to connect to the server that is listening for the right UUID a message box pops up saying:
"There was a problem pairing with bluetooth device."
And I get an exception with the message "Connection timed out." So it looks like the server and the Android are communicating, but there is some sort of failure during handshaking. I know that the Android requires that the server is paired with the phone and also encrypts the communication channel. Does anyone know which specifications are used to do this? I would love to get my server to respond properly to the connection attempt.
Thanks!
I figured out what was going wrong. The problem is that you have to pair the remote device with the Android before you try to make the connection because the pairing dialog does not always work. I am not sure if the bug is on the Android side or on the other device's side, but I was never shown the dialog that asks for the authentication code or that displays the authentication code on either device. So it looks like there are some compatibility issues out there. But if you explictly pair the devices beforehand, then the connection works.
You're right. I'm having the same issue here while trying to connect to an RS232 device with BluetoothChat sample program.
1- If not bonded, I get two errors in a row :
03-03 13:38:20.020: ERROR/BluetoothService.cpp(2077): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session) // Probably because we're calling cancelDiscovery() as a "security" (it's already been cancelled at some point beforehand)
03-03 13:38:20.040: ERROR/BluetoothEventLoop.cpp(2077): onCreateDeviceResult: D-Bus error: org.bluez.Error.AlreadyExists (Device already exists) // This I don't understand
2- Once bonded through Bluetooth Settings, no problem. The connection is made perfectly and the program works.
I tried to check out the Android Source by myself but, given that I'm a newbie, it's kind of tough. If some experienced guy could work it out, it'd be great.
To answer Tomas - The SPP (Serial Port Profile) profile is broken on several HTC Android models, even 2.1 versions, but it is not consistent among the models.
HTC Desire - Does not work.
HTC Legend - Bad, but works for a while?
HTC Hero with custom ROMs - Some work well, some not at all and some badly when doing sound at the same time.
All the previous answers are on the mark; essentially, pairing is required before connect.
However, I was getting the error even after pairing. (I had first tried to connect without pairing).
The workaround was to reboot the phone once. And now it is able to connect to the paired device fine. Sounds simple, but I spent quite some time debugging it. Hopefully useful to someone out there.