Code example for BluetoothAdapter: Communicating over Bluetooth

Methods: getAddressgetNameisDiscoveringisEnabledstartDiscovery

19
  } 
 
  private void configureBluetooth() { 
    bluetooth = BluetoothAdapter.getDefaultAdapter();
    String toastText;
    if (bluetooth.isEnabled()) {
      toastText = bluetooth.getName() + " : " + bluetooth.getAddress();
    } else { 
      toastText = "No bluetooth";
      // 监测状态 
      String actionStateChanged = BluetoothAdapter.ACTION_STATE_CHANGED;
      String actionRequestEnable = BluetoothAdapter.ACTION_REQUEST_ENABLE;
      registerReceiver(bluetoothState, new IntentFilter(actionStateChanged));
      startActivityForResult(new Intent(actionRequestEnable), 0);
    } 
 
    Toast.makeText(this, toastText, Toast.LENGTH_LONG).show();
 
    // 搜索蓝牙的等待过程 
    registerReceiver(discoveryMonitor, new IntentFilter(dStarted));
    registerReceiver(discoveryMonitor, new IntentFilter(dFinished));
 
    // 搜索蓝牙后的结果 
    registerReceiver(discoveryResult2, new IntentFilter(
        BluetoothDevice.ACTION_FOUND));
    if (!bluetooth.isDiscovering())
      bluetooth.startDiscovery();
  } 
 
  private void setupListenButton() { 
    Button listenButton = (Button) findViewById(R.id.button_listen);
    listenButton.setOnClickListener(new OnClickListener() {
Experience pair programming with AI  Get Codota for Java