I have a BLE device. Device has one button. Target is to trigger certain action in android device when device button is pressed.
My issue is, i am able to discover and pair with my BLE device via system bluetooth scanner. But when i use, BLE Scanning inside code(same as google code), i am unable to see device.
I have my Following tags in my manifest.
uses-permission android:name="android.permission.BLUETOOTH"
uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
This is my gradle setup
minSdkVersion 18, targetSdkVersion 22
Phone- Nexus 5 | Android M
Here are logs. As you can see in bold text, it discovers device, but doesnt add it. Any idea why this is happening?
BtGatt.GattService﹕ registerClient() - UUID=b7516aaa-22b1-4d8f-a71e-405e5584edcf BtGatt.GattService﹕ onClientRegistered() - UUID=b7516aaa-22b1-4d8f-a71e-405e5584edcf, clientIf=5
BtGatt.GattService﹕ start scan with filters
BtGatt.ScanManager﹕ handling starting scan
BtGatt.ScanManager﹕ configureRegularScanParams() - queue=1
BtGatt.ScanManager﹕ configureRegularScanParams() - ScanSetting Scan mode=2 mLastConfiguredScanSetting=-2147483648
BtGatt.ScanManager﹕ configureRegularScanParams - scanInterval = 8000configureRegularScanParams - scanWindow = 8000
BtGatt.GattService﹕ onScanParamSetupCompleted : 0
bt_btif_gattc﹕ btif_gattc_update_properties BLE device name=Security Tag len=12 dev_type=2 bt_btif_gattc﹕ btif_gattc_update_properties BLE device name=BlueFind len=15 dev_type=2
BtGatt.GattService﹕ stopScan() - queue size =1
BtGatt.ScanManager﹕ stop scan
BtGatt.ScanManager﹕ configureRegularScanParams() - queue=0
BtGatt.ScanManager﹕ configureRegularScanParams() - ScanSetting Scan mode=-2147483648 mLastConfiguredScanSetting=2
BtGatt.ScanManager﹕ configureRegularScanParams() - queue emtpy, scan stopped
BtGatt.GattService﹕ unregisterClient() - clientIf=5
BtGatt.GattService﹕ registerClient() -
**UUID=2f2450e9-ea7a-4dfe-aef2-27bcd75c83c5**
BtGatt.GattService﹕ onClientRegistered() - UUID=2f2450e9-ea7a-4dfe-aef2-27bcd75c83c5, clientIf=5
BtGatt.GattService﹕ start scan with filters
BtGatt.ScanManager﹕ handling starting scan BtGatt.ScanManager﹕ configureRegularScanParams() - queue=1
BtGatt.ScanManager﹕ configureRegularScanParams() - ScanSetting Scan mode=2 mLastConfiguredScanSetting=-2147483648
BtGatt.ScanManager﹕ configureRegularScanParams - scanInterval = 8000configureRegularScanParams - scanWindow = 8000
BtGatt.GattService﹕ onScanParamSetupCompleted : 0
**bt_btif_gattc﹕ btif_gattc_update_properties BLE device name=Security Tag len=12 dev_type=2**
**bt_btif_gattc﹕ btif_gattc_update_properties BLE device name=BlueFind len=15 dev_type=2**
BtGatt.GattService﹕ stopScan() - queue size =1
BtGatt.ScanManager﹕ stop scan
BtGatt.ScanManager﹕ configureRegularScanParams() - queue=0
BtGatt.ScanManager﹕ configureRegularScanParams() - ScanSetting Scan mode=-2147483648 mLastConfiguredScanSetting=2
BtGatt.ScanManager﹕ configureRegularScanParams() - queue emtpy, scan stopped
BtGatt.GattService﹕ unregisterClient() - clientIf=5
--EDIT1--
Here is my code snippet
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
invalidateOptionsMenu();
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
invalidateOptionsMenu();
}
// Adapter for holding devices found through scanning.
private class LeDeviceListAdapter extends BaseAdapter {
private ArrayList<BluetoothDevice> mLeDevices;
private LayoutInflater mInflator;
public LeDeviceListAdapter() {
super();
mLeDevices = new ArrayList<BluetoothDevice>();
mInflator = BLEScanActivity.this.getLayoutInflater();
}
public void addDevice(BluetoothDevice device) {
if(!mLeDevices.contains(device)) {
mLeDevices.add(device);
}
}
public BluetoothDevice getDevice(int position) {
return mLeDevices.get(position);
}
public void clear() {
mLeDevices.clear();
}
@Override
public int getCount() {
return mLeDevices.size();
}
@Override
public Object getItem(int i) {
return mLeDevices.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
// General ListView optimization code.
if (view == null) {
view = mInflator.inflate(R.layout.listitem_device, null);
viewHolder = new ViewHolder();
viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
final BluetoothDevice device = mLeDevices.get(i);
final String deviceName = device.getName();
if (deviceName != null && deviceName.length() > 0)
viewHolder.deviceName.setText(deviceName);
else
viewHolder.deviceName.setText(R.string.unknown_device);
viewHolder.deviceAddress.setText(device.getAddress());
return view;
}
}
// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
}
};
Do you use a BluetoothAdapter to display your device or do you use a BluetoothGattCallback in a Service ?
Do the UUID in bold correspond to your device's service UUID ?
Do you have location settings on? Android 6.0+ requires location settings. You should add to you manifest:
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Once this is done you must go into your phone settings and manually enable location permissions. This is done through Settings>Privacy and Safety>App Permissions
Don't ask my why Bluetooth needs location permissions. It doesn't make sense to me either. In fact, it still needs location settings even if the location function is turned off.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.