我已经在Android 4.3 Bluetooth Low Energy示例中实现了代码来查找设备。
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();
mBluetoothAdapter.startLeScan(mLeScanCallback);
我的清单包括
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
该设备是 Nexus 4,已刷入出厂 4.3 映像。
我曾经让这个东西工作过,但在随后的运行中我得到以下错误:
07-25 19:35:09.216 3308-3308/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.voltagex.adamtestapp/org.voltagex.adamtestapp.BluetoothTestActivity}: java.lang.SecurityException: Need BLUETOOTH permission: Neither user 10148 nor current process has android.permission.BLUETOOTH.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.SecurityException: Need BLUETOOTH permission: Neither user 10148 nor current process has android.permission.BLUETOOTH.
at android.os.Parcel.readException(Parcel.java:1431)
at android.os.Parcel.readException(Parcel.java:1385)
at android.bluetooth.IBluetoothGatt$Stub$Proxy.registerClient(IBluetoothGatt.java:708)
at android.bluetooth.BluetoothAdapter.startLeScan(BluetoothAdapter.java:1487)
at android.bluetooth.BluetoothAdapter.startLeScan(BluetoothAdapter.java:1448)
at org.voltagex.adamtestapp.BluetoothTestActivity.onCreate(BluetoothTestActivity.java:103)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
... 11 more
07-25 20:13:17.567 1219-25094/? E/BtGatt.GattService: getService() - Service requested, but not available!
07-25 20:13:29.210 1219-1219/? E/bt-btif: btif_enable_service: current services:0x100040
07-25 20:13:29.210 1219-1219/? E/bt-btif: btif_enable_service: current services:0x140040
07-25 20:13:29.220 1219-1219/? E/bt-btif: btif_enable_service: current services:0x140040
07-25 20:13:29.880 1219-5621/? E/bt-btm: BTM_SecRegister:p_cb_info->p_le_callback == 0x730d7629
07-25 20:13:29.880 1219-5621/? E/bt-btm: BTM_SecRegister: btm_cb.api.p_le_callback = 0x730d7629
07-25 20:13:29.891 1219-5593/? E/bt-btif: Calling BTA_HhEnable
07-25 20:13:29.891 1219-5593/? E/bt-btif: ## btif_config_get assert section && *section && key && *key && name && *name && bytes && type failed at line:182 ##
07-25 20:13:29.891 1219-5593/? E/bt-btif: ## btif_config_get assert section && *section && key && *key && name && *name && bytes && type failed at line:182 ##
07-25 20:13:29.891 1219-5593/? E/bt-btif: btif_storage_get_adapter_property service_mask:0x140040
07-25 20:13:29.891 1219-5593/? E/bt-btif: ## btif_config_get assert section && *section && key && *key && name && *name && bytes && type failed at line:182 ##
07-25 20:13:29.901 1219-5624/? E/bt_mct: hci lib postload completed
07-25 20:13:29.941 1219-5626/? E/BluetoothServiceJni: SOCK FLAG = 1 ***********************
07-25 20:13:29.981 1219-5631/? E/BluetoothServiceJni: SOCK FLAG = 0 ***********************
07-25 20:14:27.142 5765-5765/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at org.voltagex.adamtestapp.BluetoothTestActivity$2$1.run(BluetoothTestActivity.java:120)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)