2016-05-17 68 views

回答

0

首先確保將這些權限添加到您的應用程序清單文件:

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

現在做:

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

if (bluetoothAdapter == null) { 
    // device doesn't support bluetooth 
} 
else { 

    // bluetooth is off, ask user to on it. 
    if(!bluetoothAdapter.isEnabled()) { 
     Intent enableAdapter = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
     startActivityForResult(enableAdapter, 0); 
    } 

    // Do whatever you want to do with your bluetoothAdapter 
    Set<BluetoothDevice> all_devices = bluetoothAdapter.getBondedDevices(); 
    if (all_devices.size() > 0) { 
     for (BluetoothDevice currentDevice : all_devices) { 
      log.i("Device Name " + currentDevice.getName()); 
     } 
    } 
} 
+0

非常感謝你:) – Androider