Android 传统蓝牙(1)

这里写图片描述

1,  /** 打开蓝牙 */
private void open() {
        mBluetoothAdapter.enable();
    }
2,/** 开始扫描蓝牙设备 */
private void startScan() {
        mBluetoothAdapter.startDiscovery();
    }
3,      
@Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // 获取被点击的设备
            BluetoothDevice device = (BluetoothDevice) parent.getItemAtPosition(position);

            // 连接设备
            startConnect(device);
        }
}/** 连接到被点击的设备 */
private void startConnect(final BluetoothDevice device) {
            new Thread(){
                public void run() {
//                  BluetoothSocket tmp = null;
                    Method method;
                    try {
                        method = device.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
                        socket = (BluetoothSocket) method.invoke(device, 1);
                        outputStream = socket.getOutputStream();
                    } catch (Exception e) {
//                      setState(CONNECT_FAILED);
                        Log.e("TAG", e.toString());
                    }
//                  socket = tmp;
                    try {
                        socket.connect();
//                      isConnect = true;
                    } catch (Exception e) {
//                      setState(CONNECT_FAILED);
                        Log.e("TAG", e.toString());
                    }
                }

注册广播接受者,接收搜索道德蓝@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化view
ListView listView = (ListView) findViewById(R.id.listView);
bluetoothDevices = new ArrayList();
adapter = new BluetoothDeviceListAdapter(bluetoothDevices);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnBluetoothDeviceItemClickListener());

// 初始化蓝牙
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

// 关注新的蓝牙设备信息
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
mBluetoothReceiver = new BluetoothReceiver();
registerReceiver(mBluetoothReceiver, filter); // Don’t forget to unregister during onDestroy

}
private final class BluetoothReceiver extends BroadcastReceiver {
@Override
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 address to an array adapter to show in a ListView
            bluetoothDevices.add(device);
            adapter.notifyDataSetChanged();
        }else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            // 开始扫描设备
            toast("开始扫描设备");
            bluetoothDevices.clear();
            adapter.notifyDataSetChanged();
        }else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            // 停止扫描设备
            toast("停止扫描设备");
        }
    }


}

“`

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值