Hi I am working with embedded bluetooth device connection with my android application.
My application is installed in Android phone and phone has some paired bluetooth devices, Now I want that if any paired bluetooth device is enabled(starts/turns on) the application should receive notification.
of-course bluetooth will be enabled in phone and bluetooth devices will be within range.
please let me know How this is possible ?
Thanks!
How about using http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#ACTION_CONNECTION_STATE_CHANGED
To check all changed states and then comparing what the new state is:
STATE_DISCONNECTED, STATE_CONNECTING, STATE_CONNECTED etc.
There is nothing automatic that can detect this, you will have to make both your App and the paired device do some work to detect this. TYhere could be 2 options :
Your app could do some periodic device discovery (low frequency - else it will kill the battery) to search for the paired devices in vicinity.
The paired devices on getting enabled / starting on should put itself to be discoverable.
A better way is for the apired device to autimaically initiate a connection once it comes on to the device it was paired with, and typically on most phones by default if Bluetooth is turned on its also scanning for incoming connection , so your app can be ready to accept incoming connections.
Related
My app automates stuff on the device, based on rules. So e.g. bluetooth device xyz connected - turn volume on. That works fine. However I want to extend it to include bluetooth low energy devices. Currently it only supports devices that I can also see as paired in the system's bluetooth settings.
Does anybody have an idea on how to monitor BTLE devices as well?
So I do not want to communicate with these devices myself, I just want to know if another application communicates with one (so receive a broadcast like with the classic BT devices).
I have the need to detect if a certain mac address is connected or in range, if it is then my app assumes that you are for example at home or in your car.
From my reading on the Bluetooth docs, it all seems more to scan and try pair / connect with new devices. I don't want to do this, Just detect if a device is in range or connected. I will know the mac address up front.
for example:
if(BluetoothAdapter.IsConnectedTo("MAC ADDRESS"))
{
Toast.makeText(this,"You are in your car",Toast.Short).show();
}
In other questions they say I should set a broadcast intent for onConnected and on disconnect. However this will only trigger when connecting, This wont help me if the device is already connected.
The other option they scan for discover-able devices, But this does not show me if the phone is currently connected to that specific device?
I am developing an Android app which behaves as Bluetooth peripheral role with a service.
When I start advertising, other Android devices searching for devices offering this service, can see my device (and can pair to it without pin) - ok.
But how to enable PIN pairing?
You can force the device has to be paired, when using specific characteristics by protecting them with PERMISSION_READ_ENCRYPTED_MITM or PERMISSION_WRITE_ENCRYPTED_MITM.
The client/central side can force pairing by calling BluetoothDevice.creteBond().
The pairing method itself is determinated by the bluetooth protocol (see this thread)
You dont actually require pairing to be done to transmit/receive data via ble, take a look into this tutorial
I am writing a android/ios application that detect/search another phone that turned on Bluetooth and get those name and mac address for further functions development. The application will run as background service that keep scanning the Bluetooth device.
Problems:
For Android, the phone Bluetooth needs to turn into discoverable mode that can let another phone to scan/detect and get the name and mac address. So, I am worry about that phone user keep requesting to another phone causing disturbance because the connection pair request can let the phone popup a confirm dialog box . Is it possible to block/reject the connection request under discoverable mode programmatically? Phone can scan bluetooth device and can get the information but can not request connection while the application is running.
For iOs, there is the same problem, but there is another question that i want to ask. Can Bluetooth keep scanning into background like android background service?
Turn off the screen of android devices that the Bluetooth signal still scannable. But iOs devices can not. Is it possible to let iOs Bluetooth device still be scannable when the screen off.
Thank you very much!
You can scan for Bluetooth devices in background (even when device is locked) as long as you add that background capability to your app. Simply start scanning when your app is launched, and don't stop.
However, this uses a significant amount of battery power (scans every 20ms), and even more if there are actual Bluetooth devices nearby (because scan response may be requested).
Instead, take a look at the Region Monitoring and iBeacon functionality in the CoreLocation classes. That is meant for power efficient discovery of BLE beacons.
You will not be able to get the MAC addresses on iOS. You will be getting UUIDs.
If the user force kills the app, background scanning stops.
I used "createBond" method to pair my phone and my device with BLE.it succeed.but I took my phone away from the device at a distance, about 10 meters.My app received the broadcast: BluetoothDevice.ACTION_BOND_STATE_CHANGED, and the device status is became BluetoothDevice.BOND_NONE. Is it normal or something wrong?
after my app receive the broadcast I will unpaired the device.because I think the device is unpaired manually by user from setting activity.
10 meters is a normal range for a BLE device. You got too far from it so you couldn't maintain a connection. This is normal. With some devices the distance will be even shorter (perhaps 5m).
BluetoothDevice.BOND_NONE broadcast when phone is away from the paired device at a distance is not normal I guess.
Yes, the disconnection from the paired device when you're out of the bluetooth range is normal, but, as I said earlier in the comment, pairing and connectivity are two different things. The BLE might not be connected but, it can be shown in the paired list of Android once its paired.
And another interesting thing what I want to share in this case is, the bluetooth devices which are already paired once with an Android, are somehow remembered in Android device even if you un-pair them. I had this tested for some of my personal projects.