我想使用我在清单
中添加的蓝牙<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
接下来我这样做了:
String uid = "0000111f-0000-1000-8000-00805f9b34fb";
UUID uuid = UUID.fromString(uid.toUpperCase()); //Standard SerialPortService ID
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream();
mmInputStream = mmSocket.getInputStream();
但是在这里;
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
我遇到此错误
va.lang.NullPointerException:尝试调用虚拟方法 'null对象引用上的'android.bluetooth.BluetoothSocket
答案 0 :(得分:0)
您的Ctrl+Shift+R
实例化不正确,这就是为什么您获得<ion-app>
<ion-header>
<ion-toolbar>
<ion-title>Footer</ion-title>
</ion-toolbar>
</ion-header>
<ion-content >
<ion-router-outlet >
</ion-router-outlet>
</ion-content>
<ion-footer>
<ion-toolbar>
<ion-title>Footer</ion-title>
</ion-toolbar>
</ion-footer>
</ion-app>
的原因。可以通过简单的nullcheck来防止这种情况:mmDevice
。
NullPointerException
if(mmDevice!=null)
或者,如果您知道设备private void checkPermissions() {
ArrayList<String> permissions = ArrayList();
if (checkSelfPermission(applicationContext, ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
permissions.add(ACCESS_FINE_LOCATION);
if (checkSelfPermission(applicationContext, BLUETOOTH) != PackageManager.PERMISSION_GRANTED)
permissions.add(BLUETOOTH);
if (checkSelfPermission(applicationContext, BLUETOOTH_ADMIN) != PackageManager.PERMISSION_GRANTED)
permissions.add(BLUETOOTH_ADMIN);
if (permissions.isNotEmpty())
requestPermissions(permissions.toTypedArray(), REQUEST_PERMISSIONS);
else {
startBluetoothService();
}
}
,则可以使用BluetoothManager mBluetoothManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter mBluetoothAdapter = mBluetoothManager.adapter;
//check if bluetooth is enabled
if (mBluetoothAdapter.isEnabled){
mBluetoothAdapter.startDiscovery();
//make sure you register this receiver to properly receive callbacks
mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//Finding devices
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
doThingsWithDevice();
}
}
}
};
方法。