Scanning of Bluetooth Low Energy Fails

旧巷老猫 提交于 2019-12-03 21:25:32

问题


I am currently developing an application that will use Bluetooth Low Energy.

I am using the following link,

http://developer.android.com/samples/BluetoothLeGatt/src/com.example.android.bluetoothlegatt/DeviceScanActivity.html

I am not able to scan the surrounding BLE devices.

To scan device,

mBluetoothAdapter.startLeScan(mLeScanCallback); 

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();

                }
            });
        }
    };

回答1:


I had the same problem please add these permissions to your manifest

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>



回答2:


If You are using 23 API or higher: You have to add location permission

These all permissions in manifest:

  <uses-feature
    android:name="android.hardware.bluetooth_le"
    android:required="true" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />


来源:https://stackoverflow.com/questions/25884129/scanning-of-bluetooth-low-energy-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!