启动内置蓝牙发现活动

时间:2014-06-11 13:59:45

标签: android bluetooth

我想开始执行蓝牙扫描和配对的活动,因为您可以通过Android上的设置到达那里。

我试过在网上寻找这个,但我找到的只是startDiscovery的{​​{1}}调用,这意味着我必须实现广播接收器等。我最终会实现这些,但是现在(我被时间紧迫了),我只想用这样的BluetoothAdapter来调用那个活动:

startActivity

Intent i = new Intent(Bluetooth.ACTION_SCAN); startActivity(i); 不是我想要的,因为我不想自己被发现。

2 个答案:

答案 0 :(得分:0)

首先,您需要浏览附近的蓝牙设备并创建一个数组 您还需要以下权限:


      

你可以试试这个代码它总是对我有用,虽然它可能需要一些剪裁

private static final int REQUEST_ENABLE_BT = 1;
      BluetoothAdapter btAdapter;

  /** Called when the activity is first created. */  
  @Override  
  public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);  

    textview1 = (TextView) findViewById(R.id.textView1);  

    // Getting the Bluetooth adapter  
    btAdapter = BluetoothAdapter.getDefaultAdapter();  
    textview1.append("\nAdapter: " + btAdapter);  

    CheckBluetoothState();  
  }  

  /* It is called when an activity completes.*/  
  @Override  
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    super.onActivityResult(requestCode, resultCode, data);  
    if (requestCode == REQUEST_ENABLE_BT) {  
      CheckBluetoothState();  
    }  
  }  

  @Override  
  protected void onDestroy() {  
    super.onDestroy();  
  }  

  private void CheckBluetoothState() {  
    // Checks for the Bluetooth support and then makes sure it is turned on  
    // If it isn't turned on, request to turn it on  
    // List paired devices  
    if(btAdapter==null) {   
      textview1.append("\nBluetooth NOT supported. Aborting.");  
      return;  
    } else {  
      if (btAdapter.isEnabled()) {  
        textview1.append("\nBluetooth is enabled...");  

        // Listing paired devices  
        textview1.append("\nPaired Devices are:");  
        Set<BluetoothDevice> devices = btAdapter.getBondedDevices();  
        for (BluetoothDevice device : devices) {  
          textview1.append("\n  Device: " + device.getName() + ", " + device);  
        }  
      } else {  
        //Prompt user to turn on Bluetooth  
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);  
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);  
      }  
    }  
  }  

我假设你有一个名为textview1

的textview布局

答案 1 :(得分:0)

Intent intentBluetooth = new Intent();
intentBluetooth.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS);
startActivity(intentBluetooth);  

您可能需要此权限:android.permission.BLUETOOTH_ADMIN