В моем приложении внутри широковещательного адаптера на Bluetooth при событии я получаю список устройств пары и затем проверяю имя каждого устройства отдельно, если какое-либо устройство содержит определенную подстроку, затем попытайтесь подключиться к этому с помощью этой библиотеки 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)
если вам интересно, возможно, это ошибка библиотекитогда это не тот случай, когда я пробую часть кода внутри действия, и он работает нормально.