//获取本地蓝牙适配器信息 bluetoothAdapter=BluetoothAdapter.getDefaultAdapter(); //打开蓝牙 if(!bluetoothAdapter.isEnabled()){ intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(intent,1);//1表示打开蓝牙 } @SuppressLint("MissingPermission") Set<BluetoothDevice> pairedDevices=bluetoothAdapter.getBondedDevices(); readyDevices = new ArrayList(); //判断有没有以配对的设备 if(pairedDevices!=null&&pairedDevices.size()>0) { for (BluetoothDevice device : pairedDevices) { readyDevices.add(device); devicesNames.add(device.getName()); btnames = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, devicesNames); } btList.setAdapter(btnames); }else{ Toast.makeText(this,"没有设备已配对!",Toast.LENGTH_SHORT).show(); } //列表项目蓝牙设备名称点击,连接 btList.setOnItemClickListener(new AdapterView.OnItemClickListener() { private int position;
这是日志
04/15 15:43:14: Launching 'app' on Pixel 5 API 26.
Install successfully finished in 867 ms.
$ adb shell am start -n "com.example.ly/com.example.ly.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 22606 on device 'Pixel_5_API_26 [emulator-5554]'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/zygote: Late-enabling -Xcheck:jni
W/zygote: Unexpected CPU variant for X86 using defaults: x86
D/OpenGLRenderer: HWUI GL Pipeline
D/: HostConnection::get() New Host Connection established 0x9f464680, tid 22637
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/OpenGLRenderer: Swap behavior 0
D/EGL_emulation: eglCreateContext: 0x9f705120: maj 2 min 0 rcv 2
D/EGL_emulation: eglMakeCurrent: 0x9f705120: ver 2 0 (tinfo 0x9f703280)
D/EGL_emulation: eglMakeCurrent: 0x9f705120: ver 2 0 (tinfo 0x9f703280)
E/BluetoothAdapter: Bluetooth binder is null
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ly, PID: 22606
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ly/com.example.ly.BluetoothActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.bluetooth.BluetoothAdapter.isEnabled()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.bluetooth.BluetoothAdapter.isEnabled()' on a null object reference
at com.example.ly.BluetoothActivity.onCreate(BluetoothActivity.java:60)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
I/Process: Sending signal. PID: 22606 SIG: 9