新手:蓝牙调用 报错

一抹笑zZ 2019-04-02 04:49:58
if (!mBluetoothAdapter.isEnabled()) {
Log.v("abcdefg", "11");
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
在isEnabled()调用时报错的应该,以下为源码,好像源码不全,不知道该怎么解决了



E/BluetoothAdapter: Bluetooth binder is null
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.administrator.myapplication, PID: 10282
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.administrator.myapplication/com.example.administrator.myapplication.MainActivity}: 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:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.bluetooth.BluetoothAdapter.isEnabled()' on a null object reference
at com.example.administrator.myapplication.MainActivity.onCreate(MainActivity.java:103)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

...全文
1310 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
luj_1768 2019-04-04
  • 打赏
  • 举报
回复
好像是:要绕过系统提示启用蓝牙,会导致虚拟机宕机。即:不让用户知道,就启动不了蓝牙。
爆炸君 @ 2019-04-03
  • 打赏
  • 举报
回复
哈哈😄😄😄😄
一抹笑zZ 2019-04-03
  • 打赏
  • 举报
回复
引用 7 楼 a875752350 的回复:
[quote=引用 5 楼 一抹笑zZ 的回复:]
[quote=引用 4 楼 a875752350 的回复:]
com.example.administrator.myapplication.MainActivity.onCreate(MainActivity.java:103)
这一行变量mBluetoothAdapter是空对象


加了Log.v("abcdefg", "11");调试发下问题应该出在mBluetoothAdapter.isEnabled()这一句,看源码这也句也是会返回异常,导致程序崩溃,关键是看源码发现好多class没法查看源码,不知道这个该怎么解决[/quote]

这个,如果没有太多精力就不要去深究源码了。简单的看一下是在哪给对象赋值的,为什么没能成功赋值,一般就能找到解决办法了[/quote]

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
textview4.setText("无蓝牙");
// Device does not support Bluetooth
}
else if (!mBluetoothAdapter.isEnabled()) {
textview4.setText("有蓝牙");
Log.v("abcdefg", "11");
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
把改成else if 就好了,应该是PC调试的时候没有蓝牙,蓝牙对象就是null,就不能调用后面的isEnable(),导致报错的

Unicode Escapes 2019-04-03
  • 打赏
  • 举报
回复
引用 5 楼 一抹笑zZ 的回复:
[quote=引用 4 楼 a875752350 的回复:]
com.example.administrator.myapplication.MainActivity.onCreate(MainActivity.java:103)
这一行变量mBluetoothAdapter是空对象


加了Log.v("abcdefg", "11");调试发下问题应该出在mBluetoothAdapter.isEnabled()这一句,看源码这也句也是会返回异常,导致程序崩溃,关键是看源码发现好多class没法查看源码,不知道这个该怎么解决[/quote]

这个,如果没有太多精力就不要去深究源码了。简单的看一下是在哪给对象赋值的,为什么没能成功赋值,一般就能找到解决办法了
Stsck 2019-04-03
  • 打赏
  • 举报
回复
buildscript {     repositories {         maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }         jcenter()         google()     }     dependencies {         classpath 'com.android.tools.build:gradle:3.0.0'     } } allprojects {     repositories {         maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }         maven { url "https://jitpack.io" }         jcenter()         google()     } } task clean(type: Delete) {     delete rootProject.buildDir }
yung7086 2019-04-03
  • 打赏
  • 举报
回复
先判断一下设备是否支持ble,在打开操作蓝牙。。我觉得崩原因应该是权限问题,加入动态权限申请代码
  • 打赏
  • 举报
回复
这个我才弄过的,需要注入依赖
一抹笑zZ 2019-04-02
  • 打赏
  • 举报
回复
引用 3 楼 gxh_apologize 的回复:
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 这是什么意思


这是要启用蓝牙
一抹笑zZ 2019-04-02
  • 打赏
  • 举报
回复
引用 4 楼 a875752350 的回复:
com.example.administrator.myapplication.MainActivity.onCreate(MainActivity.java:103)
这一行变量mBluetoothAdapter是空对象


加了Log.v("abcdefg", "11");调试发下问题应该出在mBluetoothAdapter.isEnabled()这一句,看源码这也句也是会返回异常,导致程序崩溃,关键是看源码发现好多class没法查看源码,不知道这个该怎么解决
Unicode Escapes 2019-04-02
  • 打赏
  • 举报
回复
com.example.administrator.myapplication.MainActivity.onCreate(MainActivity.java:103)
这一行变量mBluetoothAdapter是空对象
gxh_apologize 2019-04-02
  • 打赏
  • 举报
回复
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 这是什么意思
一抹笑zZ 2019-04-02
  • 打赏
  • 举报
回复

源码为什么报红不能查看
一抹笑zZ 2019-04-02
  • 打赏
  • 举报
回复
有知道的吗,自己顶

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧