public class MainActivity extends Activity{
int REQUEST_ENABLE_BT = 1;
……
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initBluetooth();
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public void initBluetooth() {
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, "Not support BLE", Toast.LENGTH_SHORT).show();
}
final BluetoothManager bluetoothManager =
(BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
}
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter()
//获取已配对的蓝牙设备
Set<BluetoothDevice> devices = adapter.getBondedDevices()
for (BluetoothDevice device : devices) {
System.out.println("已配对的设备名称:" + device.getName() + "uuids" + device.getUuids())
}