I'm a newbie on Android BLE programming.
So, I need a people who have a professional skills on a BLE concept :)
I made a app which connect between apple device and android phone.
apple device is slave, so apple send advertise message to android periodically.
But I have a little confused how to find my bluetooth device between scan results.
m_lescanner.startScan(filters, new ScanSettings.Builder().setScanMode(2).build(), m_scan_callback);
My app enroll broadcast receiver to listen bluetooth events.
If the events occurs, app start scanStart function to gathering BLE device near by android phone.
private ScanCallback m_scan_callback = new ScanCallback() {
#Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
After that, I got a many BLE device results including my BLE device(apple device) But I don't know what BLE results is mine....
all MAC address is different, and all UUID is null(I'm also confused why this flags is different and results is null).
Thank you for reading my question.
PLEASE HELP!!!
Related
I implemented a Bluetooth advertiser and a receiver using Android. At the receiver I wanted to find whether unique MAC addresses are available for each and every advertisers. For the test I used a HTC mobile as the receiver and a Samsung j5 mobile. I found there are many MAC addresses appear at the receiver.
here is my code for receiver,
#Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
BluetoothDevice testDevice = result.getDevice();
String address = testDevice.getAddress();
outlbl.setText(address);
}
so I tested the surrounding with a custom app and found this,
here is the screenshot
for the same data there are many MAC addresses from the same device.
How is this possible?
I believe the devices randomize their mac addresses. These may address it further
Randomize Mac Address Bluetooth LE Broadcast
Does MAC ID broadcasted in Eddystone Beacon change?
I am using Kontakt beacon device & testing ble on nexus 5. When I tested iPad as a beacon device I am able to find out the services & characteristics with the help of the code given on android developer site. But now I want to do some actions depending on the distance of the mobile from the beacon device. So I tried to detect how far is the mobile from the beacon device like iOS CLBeacon.Proximity (CLProximityFar, CLProximityNear, CLProximityImmideate & CLProximityUnknown). I searched but not able to find out like this in android. So decided to do some actions depending on the rssi. But problem is whatever rssi I am getting first time i.e at the time of mobile detect beacon device is not changing even if I move near to beacon device or went far away from it.
If any one had idea how to get the changing rssi in the android then please share.
Thanks.
I'm not sure how you checked the never changed rssi as you described, via a existed APP or writing codes by yourself?
but from the code, it's quite straight forward, I put a core code in Android 5.0,
//get your device's BT adapter
mBluetoothAdapter = ((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();
mLescanner = mBluetoothAdapter.getBluetoothLeScanner();
settings = new ScanSettings.Builder().setScanMode(scanSettings.SCAN_MODE_LOW_LATENCY).build();
mLescanner.startScan(this.mLeScanCallback);
And the callback is like this, and you can get the real time rssi in the function:
this.mLeScanCallback = new ScanCallback() {
#Override
public void onScanResult(int callbackType, ScanResult result) {
int realTimeRssiYouWant = result.getRssi();
}
}
Is it possible to connect with other Bluetooth LE device with out scanning.
I am working on app when bluetooth is ON and then automatically received the notification when I enter in any marketPlace that Beacon device(basically Bluetooth LE) is in your range. without my scanning Bluetooth Le. My Bluetooth is just ON. no scanning.
Because our requirements are that bluetooth doesn't scan just on, when ever new BLE is in range show alert or any notification.
I implement some scan method
startScan(){}
stopScan(){}
#Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {}
but i don't want that i directly want to get connection message.
Please help me in form of pieces of code and also with little bit explanation
Thanks
You can use BluetoothAdapter.
BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(mac);
device.connectGatt(mContext, false, mGattCallback);
// TODO your code
If the bluetooth device is not around, in BluetoothGattCallback's onConnectionStateChange will report BluetoothGatt.STATE_DISCONNECTED.
BluetoothDevice's creator has packages scope. But the source code of BluetoothAdapter's getRemoteDevice is:
public BluetoothDevice getRemoteDevice(String address) {
return new BluetoothDevice(address);
}
Do you just want to discover devices that support a particular service? There is an overload startLeScan(UUID[], ...), where you can pass the UUID's of the services that you are interested in.
Otherwise, if you just want to connect to a device of a known BT address, you maybe able to create a Bluetooth device object with that address and call connectGatt() on it. Just an idea, not sure if this would work :-)
Creating a bluetooth device object with address is not possible!
BluetoothDevice's creator has packages scope. so you can not create BluetoothDevice.
And although BluetoothDevice implements Parcelable, it cannot be created from a file.
I am working on using the BT 4.0 API that Motorola has provided with the RAZR. In one of their documents it states to use the Android API to pair before connecting and using their framework. Per their instructions I have been pairing with OS Bluetooth settings application, but it never prompts me for a key. It will pair but doesn't appear to bond, and this is critical for me.
My question is, when they say "using the Android API" is this referring to simply using the OS Bluetooth utility to pair before hand (like I have been doing), or is there some way to do it with code in my application. They reference the "createBond()" function which, to my knowledge, is not an accessible function (at least not without some squirrely libraries or reflection).
Any advice is greatly appreciated, especially anyone who has used the API successfully, if they could give an account of their process. I'm just looking for some clarity at this point :)
Lloyd,
You are correct, follow the instructions in the link you posted.
Outside of coding, when they say use the standard android api for "non-le" operations, they mean go ahead and pair the ble device the same way you would any bluetooth classic devices inside android settings -> wireless & network -> bluetooth -> scan for devices.
If the device you are using is a motorola le compatible device the ble device will be paired but not connected.
Now, in the code, you can detect this paired device through the same method of
BluetoothAdapter.getDefaultAdapter().getBondedDevices()
To double check if your Android Phone is LE compatible, run this code:
public static boolean checkBLESupport() {
boolean deviceSupportsLE;
try {
#SuppressWarnings({ "unused", "rawtypes" })
Class object = Class.forName("android.server.BluetoothGattService");
deviceSupportsLE = true;
} catch (Exception e) {
deviceSupportsLE = false;
}
return deviceSupportsLE;
}
And to double check if the bluetooth device you paired is LE, when you are looping through the bonded devices.
Check the device with this code.
if (device.getBluetoothClass() == null) {
Log.i(TAG, "This device is BLE compatible");
b = true;
} else {
Log.i(TAG, "This device is not BLE");
b = false;
}
Now for establishing connection from your LE compatible phone to your LE compatible bluetooth device, follow the Gatt service instructions under the link you posted. http://developer.motorola.com/docs/bluetooth-low-energy-api/
Take note that under this example it is connecting to a bluetooth low energy heart rate monitor.
If you are not trying to connect to the heart rate monitor with LE heart rate profile, here is a link to another Motorola document that details creating your own LE Profile to use with the GATT framework. http://developer.motorola.com/docs/bluetooth-low-energy-gatt-framework-api/
If the instructions are not clear enough at any point in either of these documents, motorola offers sample android applications using the frameworks in those documents.
I guess motorola stack has BLE support. But what i feel is that it does not pair with the devices that require bonding though It does work some sensors. I have tried with a proximity sensor that require bonding. It never gets paired though the devices is discovered with Razr which even does not with S3.
There's a helpful video here.
Late to the game, but can confirm -
If your BLE Peripheral requires bonding, Moto X - and some other older Motorola devices - MUST be paired via Bluetooth Settings prior to programmatic connection via the Android GATT interface.
If you bond via the createBond method, or reading of an encrypted characteristic, your connection will be dropped typically in under 60 seconds, despite DDMS logs that show a good bond may be established.
Alright, I've got a bit of a weird question here. I'm working on an Android game where I'd like to be able to have Android phones detect the presence of each other.
The device searching for other players will know the bluetooth mac addresses of the other players' devices (from a game DB), however the devices will not be paired and the devices will not be in discoverable mode. Also, there will only be a handful of devices that could possibly be found - so it's not a big deal to scan through mac addresses.
I don't need to connect to the devices, I just need to be able to answer one simple question: is this device with this mac address nearby?
It is permissible to have a pairing dialog appear on the other user's screen...I don't care what the outcome of their choice is...I just need to know if their device is there.
Any help would be greatly appreciated!
This use-case may be a good fit for the recently released Nearby API. See the Nearby Messages developer overview
Nearby has its own runtime permission saving you from adding BLUETOOTH_ADMIN or similar to your manifest. It works across iOS and Android by utilizing multiple technologies (Classic Bluetooth, BLE, ultrasound). There's an option to use only the ultrasonic modem which reduces the range to about 5 feet.
I've included a partial example below, you can find a more complete sample on github
// Call this when the user clicks "find players" or similar
// In the ResultCallback you'll want to trigger the permission
// dialog
Nearby.Messages.getPermissionStatus(client)
.setResultCallback(new ResultCallback<Status>() {
public void onResult(Status status) {
// Request Nearby runtime permission if missing
// ... see github sample for details
// If you already have the Nearby permission,
// call publishAndSubscribe()
}
});
void publishAndSubscribe() {
// You can put whatever you want in the message up to a modest
// size limit (currently 100KB). Smaller will be faster, though.
Message msg = "your device identifier/MAC/etc.".getBytes();
Nearby.Messages.publish(googleApiClient, msg)
.setResultCallback(...);
MessageListener listener = new MessageListener() {
public void onFound(Message msg) {
Log.i(TAG, "You found another device " + new String(msg));
}
});
Nearby.Messages.subscribe(googleApiClient, listener)
.setResultCallback(...);
}
Disclaimer I work on the Nearby API