Android蓝牙开发

蓝牙设备的权限

首先需要在AndroidManifest.xml中加入这两个蓝牙权限

android.permission.BLUETOOTH

android.permission.BLUETOOTH_ADMIN


打开蓝牙

创建BluetoothAdapter对象bluetoothAdapter

private BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter;

通过此方法可以直接打开蓝牙

public void onClickOpenBluetooth(View view){
     bluetoothAdapter.enable();
}

或使用此方法,会有对话框提示

public  void onClickOpenBluetoothOnDialog(View view){
    Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(intent,1);
}

关闭蓝牙

此方法可以直接关闭蓝牙

public void onClickCloseBluetooth(View view){
    bluetoothAdapter.disable();
}

搜索蓝牙设备

布局文件main.xml

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:onClick="onClick_Search"
        android:text="开始扫描" />
<ListView
        android:id="@+id/lv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

MainActivity.java

//定义好这些控件
private ListView lvDevices;
private List<String> bluetoothDevices = new ArrayList<String>();
private ArrayAdapter<String> arrayAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.main);
    //获取系统已经绑定的蓝牙设备,方法插入到onCreate()方法中
    Set<BluetoothDevice> pairedDevices =bluetoothAdapter.getBondedDevices();
        if (pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {
                bluetoothDevices.add(device.getName() + ":"+ device.getAddress() + "\n");
            }
        }
        arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1,bluetoothDevices);
        lvDevices.setAdapter(arrayAdapter);        
        //注册搜索时的蓝牙设备的receiver
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        this.registerReceiver(receiver, filter);
        //注册搜索完成时的蓝牙设备的receiver
        filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        this.registerReceiver(receiver, filter);
        }
    /**
     * 搜索蓝牙设备
     *
     * @param  view
     */
    public void onClick_Search(View view) {
        setProgressBarIndeterminateVisibility(true);
        setTitle("搜索设备");
        bluetoothDevices.clear();
        if (bluetoothAdapter.isDiscovering()) {
            bluetoothAdapter.cancelDiscovery();
        }
        bluetoothAdapter.startDiscovery();
    }
    /**
     * 写一个广播
     */
    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            //如果检测到现在是扫描状态
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                //搜索设备
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                //如果搜索到的设备不是已经绑定的设备
                if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                    //将搜索到的设备的名称和地址显示出来
                    bluetoothDevices.add(device.getName() + ":" + device.getAddress());
                    //通知适配器设置更改
                    adapter.notifyDataSetChanged();
                }
            }
            //搜索完成
            else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                setTitle("扫描完成");
                setProgressBarIndeterminateVisibility(false);
            }
        }
    };












你可能感兴趣的:(android,蓝牙)