I am creating an application for communicating Android mobile devices with Bluetooth beacons. I have successfully achieved this between one beacon (which acts as a client) and one android mobile device (acts as the server, listening for the connection). Please find the code snippet below,
Client app
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(MACaddress);
BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(UUID.fromString("f3c74f47-1d38-49ed-8bbc-0369b3eb277c"));
mBluetoothAdapter.cancelDiscovery();
socket.connect();
Server app
mmServerSocket = mBluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord("NAME", UUID.fromString("f3c74f47-1d38-49ed-8bbc-0369b3eb277c"));
socket = mmServerSocket.accept();
This works fine with one mobile device (server) where I am able to obtain its MACaddress by scanning nearby Bluetooth devices. But consider the case when there are around 20-25 mobile devices present in range - scanning would consume so much time. I would like to do this without performing a scan.
Is there a way to achieve this without using the server's MACaddress? Can the connection be established with the unique UUID alone? Please note that I do not want to pair the devices.
Any other solution in this regard is also welcome. Thanks!
You can't create a socket connection to a bluetooth device without identifying the bluetooth device itself, and the unique identifier for bluetooth device is a MAC Address. Even if there were an Android API that would allow you to do this via UUID alone, the underlying implementation would still have to search for nearby devices (e.g. scan) and find one that is accepting socket connections using the UUID. So the scanning mentioned in the question would still have to happen.
Yes, scanning can use a bit of power and CPU intensive if a number of bluetooth devices are around. You can limit this two ways:
Only scan briefly. For your use case, it seems you could just scan for a few seconds then stop once you find a supporting device. This should not be too resource intensive.
Use scan filters on Android 5.0+ to match only the devices that you are interested in.
Related
I have a setup with two Android devices: an android phone and a custom hardware running Android 10. The latter acts as a BLE Server, which is discoverable always with the same static MAC address. Also when that custom HW is paired with the phone it has access to phone's contacts and calls history. That HW can also play media (sound) streamed from the phone.
I use RxAndroidBle 1.11.0 library for BLE communication. As soon as I exchange some data via BLE Characteristic I unsubscribe from the RX observable so the library effectively calls: bluetoothGatt.disconnect() then blutetoohGatt.close()
(all those inside the DisconnectOperation class).
My problem is the fact that based on my observations, calling blutetoohGatt.disconnect() disconnects the classic BT profile (PBAP) as well.
Is it an expected behaviour?
I have investigated the code of generic Android P framework and it looks that calling bluetoothGatt.disconnect() -> bluetoothGatt.close() completely disconnects the device described by the given MAC Address:
BluteoothGatt#disconnect()
BlutetoothGatt#close()
BlutetoothGatt#unregisterApp()
I would risk a statement that I have quite unusual BLE use-case scenario where the phone (Central/Client) and the peripheral (custom Android HW/Server) are connected simultaneously via Classic BT profile and via BLE.
I suspect that disconnecting/closing device using its BT MAC address disconnect both profiles, hence the contacts sharing stops working.
Is it possible to disconnect BLE connection only, but not affect the classic BT (SPP/PBAP) connections between the two devices?
After further investigation described on that Github issue
I realised that my custom hardware is equipped with dual-mode BT adapter so it supports classic BT (BR/EDR) and the BLE one.
As soon as I pair an Android phone with the custom hardware via System Settings, classic BT profiles are bonded, but also my GATT Server advertises relevant services using the same MAC address which is used by the BR/EDR. Disconnecting BLE client connection disconnect BD/EDR as well.
What helps in my case is to not start pairing via System Settings, but let the GATT server advertise and let my BLE client app to connect (and trigger pairing) first. As a result I have a BLE connection with server available under "AA:AA:AA:AA:xx", while the classic BT profiles are connected to "BB:BB:BB:BB:yy" interface on the custom hardware.
Now, disconnecting BLE profile does not disconnect the classic one.
I am looking for a solution which would allow my GATT Server to be advertised with BLE only, but no luck so far.
Here are some interesting links I have found:
connectGatt creates connection over BT Classic instead of BLE
how to force BLE "just works" pairing in Android
BLE Dual-Mode
I'am trying to make an app that receives some binary data from a other device which is not running android. The Android phone should not transfer any data to this device. So over to my questions:
Should the phone act as the server which establishes the RCOMM channel?
Is it possible to connect the phone and this device without paring, as long as I know the MAC-address of the device? If so, how?
And yes, I have read http://developer.android.com/guide/topics/connectivity/bluetooth.html so no need to only answer with that link. :)
Thanks in advance!
1)
Phone should act as a receiver.
I found that the tricky part is to use correct UUID.
If you connect two phones you should be generating those and everything work fine, but on your hardware you can't change UUID that easily.
Take a look at these reserved UUIDs.
For a Serial Port Profile's (SPP) communication use UUID 00001101-0000-1000-8000-00805F9B34FB.
2)
You can connect to a device without pairing if you already know the MAC address, you don't even need to use BlueTooth discovery.
To get device using MAC address call getRemoteDevice(String)
and then createRfcommSocketToServiceRecord(UUID) to get BlueToothSocket.
Also take a look at checkBluetoothAddress(String) to validate MAC address.
I am trying to use Bluetooth SPP to communicate over it. In some Android phones, SPP profile is not activated. I faced the problem in the application that SPP was not activated and so the connection could not be established over bluetooth and when I started another app that is 3g hotspot which I think activates SPP and I was able to connect over bluetooth in my app.
So, how can we actually activate SPP profile of bluetooth in Android devices? And does all the android devices has SPP profile?
In developer.android.com BluetoothSocket:
The most common type of Bluetooth
socket is RFCOMM, which is the type
supported by the Android APIs. RFCOMM
is a connection-oriented, streaming
transport over Bluetooth. It is also
known as the Serial Port Profile
(SPP).
This might answer your question ..
https://source.android.com/devices/bluetooth/services
SPP (or ability to use Java API's to establish RFCOMM channels) is available from Android (2.0/2.1) release (Eclair) onwards
On the Android phone you will probably need to run an application that initiates the service over SPP.
**
public BluetoothServerSocket
listenUsingRfcommWithServiceRecord
(String name, UUID uuid)
** API can be used to create a service with specified UUID to listen,
Doing this should make this service visible to other devices which can then connect to it.
I just called Samsung help regarding bluetooth SPP on an S8+ phone. They tell me wait for Android Oreo as it isn't available till then. My previous Sony Xperia used Ntrip Client to talk to an external Geneq GPS to receive corrected coordinates and worked really well. Seems we went backwards somewhere?
Ntrip client on the phone collects an internet data stream of GPS corrections every five seconds and sends these to the GPS. The GPS uses the corrections and sends back adjusted coordinates to the phone to use in any application.
I would appreciate someone brighter than me pointing out a quick fix I just load and run to bring back this functionality.
If I wanted to connect to another device via bluetooth how would I do it?
Nothing I've done seems to work. The phone I'm trying to connect to is non android and has bluetooth on and discoverable.
Bluetooth API is realy poor in this case... let's have look at java-doc:
Hint: If you are connecting to a
Bluetooth serial board then try using
the well-known SPP UUID
00001101-0000-1000-8000-00805F9B34FB.
However if you are connecting to an
Android peer then please generate your
own unique UUID.
from: link text
According to this, your peer should be visible under the 00001101-0000-1000-8000-00805F9B34FB service record.
Now, if you want to create an app which need to connect to Bluetooth devices, I advice you to use Cordova platform and install a plugin that let you connect via bluetooth, list devices, send data, disconnect etc...
In Bluetooth, there is a concept of service profile for an application layer. An application on a peer 1 willing to talk to same application on peer 2 must be discoverable and connectable, the master initiating the connection.
The application is given a unique service/application identifier so that peers can discover each other. If you write a custom application yourself, you are expected to create a new UUID so that the corresponding SDP protocol can perform the necessary discovery.
Instead of connecting with brute force using the BT_ADDR, you would essentially initiate connection via graceful service discovery and then connection follows once the underlying UUID is identified. This, of course, assumes that you would have done the pairing process early, or that your lower layer stack will include pairing process as well.
Can someone explain a couple of very simple concepts to me - I'm interested in mobile devices running android and how they are identified over networks. Some scenarios:
Device is connected over WiFi - presumably the device has a standard IP address as with any host and can communicate with any other android host over TCP/IP (assuming it knows the participating device's IP?
Device is connected over bluetooth - how are devices identified in this case?
Device is connected over mobile operator's network - this is the one I'm interested in and confused by - is there anyway for two or more devices to discover each other and communicate via the mobile operator's network? How does a device communicate with a backend server in this scenario? In other words, how do apps and devices communicate when not connected to a WiFi network?
Thanks for any advice..
I'm only sure about the bluetooth thing, so i only answer this part:
The Bluetooth interface on your device has an MAC adresse. So while communicationg over Bluetooth you can assume that this MAC adresse is a unique identifier for a specific device. You can also reach other devices by establishing a connection over this MAC adress- However, to get this mac adress in the first place, you have to know it from somwhere, or you have to search for other bluetooth devices in the reachabla area before.
WiFi and 3g both attach the [mobile] device to the internet so it can make internet connections. 3G assigns a publicly addressable IP to the device, so one could, presumably open a server socket and listen for connections. The client would have to know the mobile IP, which may change quite frequently.
Bluetooth is more geared toward close-proximity. Devices in the vicinity can be connected to, after you have paired with them, which requires the cooperation of both devices which are to communicate.
If the goal is to produce an application which connects to nearby devices, I can think of the following ideas:
3g: all devices running the client register their position with a central database server. If the server detects that two clients are in close proximity, let them know so they can connect through the internet or through the server
WiFi: you could use the same idea as 3g, or use broadcast/multicast packets to broadcast your presence. Other apps can listen for those broadcasts and discover which other devices are near.
Bluetooth: A little trickier, as a device must be placed into discoverable mode in order for others to "see it". Discoverable mode is a temporary state and only lasts about 30 seconds (at a time).