2017-12-21 27 views
1

Android 문서에서 다음 코드를 복사했지만 기기를 찾지 못했습니다. 왜 그런지 알아?블루투스 기기 스캔

공용 클래스 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 + ""); 
    } 
} 

페이스트 빈은 : https://pastebin.com/CnEBAtwt

작동 무엇 : -

"최종 검색을" "발견을 시작"에 대한 방송 - 경계 장치 목록 작동하지 않는 것 : - 주변 기기 검색

당신은 발견 장치에 다음 권한 중 하나를 추가 할 필요가

답변

0

감사 :

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

또는

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

는 API 23에 노력하고 높은이 권한을 확보해야하는 경우 그것이 위험한 수준의 권한이기 때문에 부여됩니다. 응용 프로그램이 실행되는 동안 안드로이드 6.0 (API 레벨 23) 년부터

는 사용자가 응용 프로그램에 권한을 부여, 그들은 응용 프로그램을 설치하지 않을 경우

권한을 요청하는 this guide를 참조하십시오.