安卓开发笔记-蓝牙开发

一般情况下用默认的蓝牙适配器就好了

private static BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

贴几个常用的方法

    /**
     * 打开蓝牙功能
     */
    public static void openBluetooth() {
        adapter.enable();
    }

    /**
     * 关闭蓝牙功能
     */
    public static void closeBluetooth() {
        adapter.disable();
    }

    /**
     * 蓝牙是否打开
     * @return
     */
    public static boolean isBluetoothOpen(){
        return adapter.isEnabled();
    }

用广播接收扫描到的设备 然后用Handle将信息发送出去

        IntentFilter filter=new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        filter.setPriority(Integer.MAX_VALUE);
        registerReceiver(receiver,filter);
public class BluetoothReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action=intent.getAction();
        if (BluetoothDevice.ACTION_FOUND.equals(action)){
            BluetoothDevice bluetoothDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (bluetoothDevice.getBondState()!=BluetoothDevice.BOND_BONDED){
                Message msg=new Message();
                msg.what=1;
                msg.obj=new com.hiep.pump.model.BluetoothDevice(bluetoothDevice.getName(),bluetoothDevice.getAddress());
                DeviceListDialog.getHandler().sendMessage(msg);
            }
            if (bluetoothDevice.getBondState()==BluetoothDevice.BOND_BONDED){
                Message msg=new Message();
                msg.what=2;
                msg.obj=new com.hiep.pump.model.BluetoothDevice(bluetoothDevice.getName(),bluetoothDevice.getAddress());
                DeviceListDialog.getHandler().sendMessage(msg);
            }
        }else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
            Message msg=new Message();
            msg.what=0;
            msg.obj="discovery finished";

            DeviceListDialog.getHandler().sendMessage(msg);
        }
    }
}

拿到蓝牙地址后 通过反射进行蓝牙配对

android.bluetooth.BluetoothDevice btDev = bluetoothadapter.getRemoteDevice(list.get(position).getAddress());
                if (btDev.getBondState() == android.bluetooth.BluetoothDevice.BOND_NONE) {
                    try {
                        Log.i("BlueToothTestActivity", "开始配对");
                        Method createBondMethod = android.bluetooth.BluetoothDevice.class.getMethod("createBond");
                        createBondMethod.invoke(btDev);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }else if(btDev.getBondState() == android.bluetooth.BluetoothDevice.BOND_BONDED){
                    connect(btDev);
                }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值