无法初始化广播接收器内的蓝牙适配器

时间:2019-03-20 05:02:47

标签: android bluetooth

在事件发生时在蓝牙上的广播适配器内部的应用程序中,我获取了一对设备列表,然后分别检查每个设备的名称(如果有设备包含特定的子字符串),然后尝试使用此lib https://github.com/OmarAflak/Bluetooth-Library与该设备连接。在那个越来越错误。

广播代码:

public class BrodcastBlueTooth extends BroadcastReceiver {

    public BrodcastBlueTooth() {

    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Bluetooth bluetooth;
        bluetooth = new Bluetooth(context);
        String DeviceName=null;
        String action = intent.getAction();
//        Log.d("BroadcastActions", "Action "+action+"received");
        int state;
        BluetoothDevice bluetoothDevice;
        ArrayList<String> pairedDevice;
        pairedDevice=new ArrayList<>();
        final BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();
        Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();

        switch(action)
        {
            case BluetoothAdapter.ACTION_STATE_CHANGED:
                state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
                if (state == BluetoothAdapter.STATE_OFF)
                {
//                    Toast.makeText(context, "Bluetooth is off", Toast.LENGTH_SHORT).show();
                    Log.d("BroadcastActions", "Bluetooth is off");
                }
                else if (state == BluetoothAdapter.STATE_TURNING_OFF)
                {
//                    Toast.makeText(context, "Bluetooth is turning off", Toast.LENGTH_SHORT).show();
                    Log.d("BroadcastActions", "Bluetooth is turning off");
                }
                else if(state == BluetoothAdapter.STATE_ON)
                {
                    Toast.makeText(context, "Bluetooth is On", Toast.LENGTH_SHORT).show();

                    if (pairedDevices.size() > 0) {
                        for (BluetoothDevice device : pairedDevices) {
                            pairedDevice.add(device.getName());
                        }
                        for(int i=0;i<pairedDevice.size();i++){
                            String name = pairedDevice.get(i);
                            if (name.contains("OBD")){
                            bluetooth.connectToName(name);
                            }else if (name.contains("obd")){
                                String Tname=name;
                                Log.d("Tname", Tname);
                                bluetooth.connectToName(name);
                            }else{
                                Toast.makeText(context,"No device which has obd name",Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                    Log.d("DevicePaired", String.valueOf(pairedDevice));

                }
                break;


        }

    }
}

日志:

2019-03-17 10:16:35.457 17502-17502/com.example.bluetoothlibraryexample E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.bluetoothlibraryexample, PID: 17502
    java.lang.RuntimeException: Unable to start receiver com.example.bluetoothlibraryexample.BrodcastBlueTooth: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Set android.bluetooth.BluetoothAdapter.getBondedDevices()' on a null object reference
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:3047)
        at android.app.ActivityThread.-wrap18(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1561)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6126)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Set android.bluetooth.BluetoothAdapter.getBondedDevices()' on a null object reference
        at me.aflak.bluetooth.Bluetooth.connectToName(Bluetooth.java:141)
        at me.aflak.bluetooth.Bluetooth.connectToName(Bluetooth.java:150)
        at com.example.bluetoothlibraryexample.BrodcastBlueTooth.onReceive(BrodcastBlueTooth.java:67)
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:3040)
        at android.app.ActivityThread.-wrap18(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1561) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6126) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

如果您想知道可能是库的错误,那么我不是在活动内尝试一段代码,并且运行良好。

1 个答案:

答案 0 :(得分:0)

看起来像BluetoothAdapter.getDefaultAdapter();可能返回null

查看有关BluetoothAdapter的文档here

  

返回默认的本地适配器,如果此硬件平台不支持蓝牙,则返回null

您确定您的设备支持蓝牙吗?启用了吗?您是否已将permissions设置为在AndroidManifest中运行蓝牙?