1
次のコードをAndroidのドキュメントからコピーしましたが、デバイスを検出しませんでした。誰でも知っている理由は? https://pastebin.com/CnEBAtwtBluetoothを搭載したデバイスをスキャンする
何作品: "スタート発見" と "最後の発見"
のための放送 - - 有界デバイス の一覧 を
パブリッククラスMainActivityはAppCompatActivity {
private BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private TextView tvBoundedDevices;
private TextView tvDiscoveredDevices;
private IntentFilter intentFilter = new IntentFilter();
private BroadcastReceiver broadcastReceiver;
private ProgressDialog progressDialog;
private ArrayList<BluetoothDevice> bluetoothDevicesFound = new ArrayList<>();
private Set<BluetoothDevice> bluetoothDevicesBounded;
@Override
protected void onDestroy() {
unregisterReceiver(broadcastReceiver);
bluetoothAdapter.cancelDiscovery();
super.onDestroy();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvBoundedDevices = findViewById(R.id.tvBoundedDevices);
tvDiscoveredDevices = findViewById(R.id.tvDiscoveredDevices);
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action){
case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
progressDialog = ProgressDialog.show(MainActivity.this,"Attendere","Scan in corso");
break;
case BluetoothDevice.ACTION_FOUND:
bluetoothDevicesFound.add((BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE));
break;
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
if(progressDialog.isShowing())
progressDialog.dismiss();
break;
default:
Toast.makeText(context, "Action=" + action, Toast.LENGTH_LONG).show();
}
}
};
registerReceiver(broadcastReceiver,intentFilter);
}
public void turnBluetoothOn(View view){
if(!bluetoothAdapter.isEnabled()){
bluetoothAdapter.enable();
Toast.makeText(this,"Bluetooth attivato",Toast.LENGTH_SHORT).show();
}
}
public void turnBluetoothOff(View view){
if(bluetoothAdapter.isEnabled()){
bluetoothAdapter.disable();
Toast.makeText(this,"Bluetooth disattivato",Toast.LENGTH_SHORT).show();
}
}
public void scanDevices(View view){
turnBluetoothOn(null);
if(bluetoothAdapter.isDiscovering())
bluetoothAdapter.cancelDiscovery();
Toast.makeText(this, "Scansione " + (bluetoothAdapter.startDiscovery()?"":"non") + " avviata.", Toast.LENGTH_SHORT).show();
printDevices();
}
String toStamp = "";
private void printDevices(){
bluetoothDevicesBounded = bluetoothAdapter.getBondedDevices();
if(!bluetoothDevicesBounded.isEmpty()){
toStamp = "Dispositivi associati:\n";
for(BluetoothDevice b : bluetoothDevicesBounded){
toStamp += b.getName() + " | " + b.getAddress() + "\n";
}
tvBoundedDevices.setText(toStamp + "");
}
if(!bluetoothDevicesFound.isEmpty()){
toStamp = "Dispositivi trovati:\n";
for(BluetoothDevice b : bluetoothDevicesFound){
toStamp += b.getName() + " | " + b.getAddress() + "\n";
}
tvDiscoveredDevices.setText(toStamp + "");
}
}
ペーストビンを拡張します何がうまくいかない: - 付近の発見デバイス
はあなたが検出されたデバイスに次の権限のいずれかを追加する必要が