Код: Выделить всё
public class ScanList extends AppCompatActivity {
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BroadcastReceiver mReceiver;
int REQUEST_ENABLE_BT = 1;
ArrayAdapter mArrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan_list);
if (mBluetoothAdapter != null) {
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this,
new String[]{Manifest.permission.BLUETOOTH_CONNECT}, REQUEST_ENABLE_BT
);
}
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver, filter);
mBluetoothAdapter.startDiscovery();
Log.i("devi", "1"); // can only print this line
}
// Create a BroadcastReceiver for ACTION_FOUND.
private BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Log.i("devi", "2");
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (ActivityCompat.checkSelfPermission(ScanList.this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
ScanList.this,
new String[]{Manifest.permission.BLUETOOTH_CONNECT}, REQUEST_ENABLE_BT
);
}
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
Log.i("devi", deviceName);
Log.i("devi", deviceHardwareAddress);
Intent reintent = new Intent(ScanList.this, MainActivity.class);
Bundle extras = new Bundle();
extras.putString("name",deviceName);
extras.putString("address",deviceHardwareAddress);
reintent.putExtras(extras);
startActivity(reintent);
finish();
}
}
};
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}
Я могу получить устройство сейчас, добавив
Код: Выделить всё
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
MainActivity.this,
new String[]{Manifest.permission.BLUETOOTH_CONNECT}, 1001
);
}
Bluetooth продолжает останавливаться
Bluetooth — это имя моего приложения. Как я могу это решить?
Edit2
Я решил это. Проблема в том, что не у каждого устройства есть имя. Если он сканирует устройство без имени и отображает его в logcat, он выдает ноль и происходит сбой. Чтобы решить эту проблему, просто добавьте оператор if и проверьте, имеет ли getName значение null.
Подробнее здесь: https://stackoverflow.com/questions/773 ... streceiver
Мобильная версия