发现蓝牙样本

时间:2017-12-24 01:28:48

标签: java android bluetooth

我正在学习蓝牙应用程序以及我遇到的第一个样本,似乎有很好的文档,但我不能为我的生活让它重现"搜索设备&#34 ;部分,任何帮助将不胜感激。

 public class MainActivity extends AppCompatActivity {

private Button onBtn, offBtn, listBtn, findBtn;
private TextView text;
private ListView myListView;

// Bluetooth global variables
private static final int REQUEST_ENABLE_BT = 1;
private BluetoothAdapter myBluetoothAdapter;
public Set<BluetoothDevice> pairedDevices;
private ArrayAdapter<String> BTArrayAdapter;
@Override


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

        myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        if(myBluetoothAdapter == null) {
            Toast.makeText(getApplicationContext(),"Your device does not support Bluetooth",
                    Toast.LENGTH_LONG).show();
        }
        else
        {

            text = (TextView) findViewById(R.id.text);
            onBtn = (Button)findViewById(R.id.turnOn);
            offBtn = (Button)findViewById(R.id.turnOff);
            listBtn = (Button)findViewById(R.id.paired);
            findBtn = (Button)findViewById(R.id.search);

            myListView = (ListView)findViewById(R.id.listView1);

            // create the arrayAdapter that contains the bluetooth d evices, and set it to the ListView
            BTArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
            myListView.setAdapter(BTArrayAdapter);

            myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    String selectedFromList =(String) (myListView.getItemAtPosition(i));
                    Toast.makeText(getApplicationContext(),"Bluetooth remote device : " + selectedFromList,
                            Toast.LENGTH_LONG).show();
                }
            });



        }

    } // onCreate


public void on(View view){
    if (!myBluetoothAdapter.isEnabled()) {
        Intent turnOnIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(turnOnIntent, REQUEST_ENABLE_BT);

        Toast.makeText(getApplicationContext(),"Bluetooth turned on" ,
                Toast.LENGTH_LONG).show();
    }
    else{
        Toast.makeText(getApplicationContext(),"Bluetooth is already on",
                Toast.LENGTH_LONG).show();
    }
}

public void off(View view){
    myBluetoothAdapter.disable();
    text.setText("Status: Disconnected");

    Toast.makeText(getApplicationContext(),"Bluetooth turned off",
            Toast.LENGTH_LONG).show();
}

public void list(View view){
    // get paired devices
    pairedDevices = myBluetoothAdapter.getBondedDevices();

    // put it's one to the adapter
    for(BluetoothDevice device : pairedDevices)
        BTArrayAdapter.add(device.getName()+ "\n" + device.getAddress());

    Toast.makeText(getApplicationContext(),"Show Paired Devices",
            Toast.LENGTH_SHORT).show();

}

final BroadcastReceiver bReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        // When discovery finds a device
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // add the name and the MAC address of the object to the arrayAdapter
            BTArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            BTArrayAdapter.notifyDataSetChanged();
        }
    }
};

public void find(View view) {
    if (myBluetoothAdapter.isDiscovering()) {
        // the button is pressed when it discovers, so cancel the discovery
        Toast.makeText(getApplicationContext(),"Bluetooth cancelled discovery" ,
                Toast.LENGTH_LONG).show();
        myBluetoothAdapter.cancelDiscovery();
    } else {
        BTArrayAdapter.clear();
        Toast.makeText(getApplicationContext(),"Bluetooth discovery started" ,
                Toast.LENGTH_LONG).show();

        myBluetoothAdapter.startDiscovery();

        registerReceiver(bReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
    }

}

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

}

使用Oneplus3&amp; 2测试这个,除非已经相互配对,否则两个设备都不能找到另一个。我想看看我是否可以搜索并让设备填充在配对设备已经存在的列表中。

如果还有其他任何你需要我让我知道的事情,希望你不要介意花些时间来帮助我解决这个问题!

1 个答案:

答案 0 :(得分:0)

您需要向Manifest.xml添加以下权限之一才能找到设备:

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

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

如果您正在使用API​​ 23及更高版本,则必须确保授予此权限,因为它是危险级别权限。

  

从Android 6.0(API级别23)开始,用户在应用运行时向应用授予权限,而非在安装应用时授予

请参阅this guide以请求许可。