Android蓝牙 *** 作

Android蓝牙 *** 作,第1张

概述1.添加蓝牙权限<uses-permissionandroid:name="android.permission.BLUETOOTH"/><!--启用应用启动设备发现或者 *** 作蓝牙设备的超级管理员--><uses-permissionandroid:name="android.permission.BLUETOOTH_ADMIN"/> 2.获取蓝牙设备bluetoothAdapter=Bl

1.添加蓝牙权限

 <uses-permission androID:name = "androID.permission.BLUetoOTH"/>    <!--启用应用启动设备发现或者 *** 作蓝牙设备的超级管理员-->    <uses-permission androID:name="androID.permission.BLUetoOTH_admin"/>

 

2.获取蓝牙设备

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();if (bluetoothAdapter == null) {                    Toast.makeText(MainActivity.this, "未找到设备", Toast.LENGTH_LONG).show();                    return;                }if (!bluetoothAdapter.isEnabled()) {                    Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);                    startActivityForResult(enableIntent, REQUEST_ENABLE_BT);                }

 

3.搜索设备

Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();        if (pairedDevices.size() > 0) {            // Loop through paired devices            for (final BluetoothDevice device : pairedDevices) {                // Add the name and address to an array adapter to show in a ListVIEw                final button btnDevice = findVIEwByID(R.ID.device_btn);                btnDevice.setText(device.getAddress());                btnDevice.setonClickListener(new VIEw.OnClickListener() {                    @OverrIDe                    public voID onClick(VIEw v) {try {                            new ConnectThread(device).run();                        } catch (IOException e) {                            e.printstacktrace();                        }                    }                });            }        }

 

4.连接设备

private class ConnectThread extends Thread {        private BluetoothDevice mDevice;        private BluetoothSocket mSocket;        public ConnectThread(BluetoothDevice device) throws IOException {            mDevice = device;            mSocket = device.createInsecureRfcommSocketToServiceRecord(UUID.randomUUID());            Log.d("aaa", "start");        }        public voID run() {            // 关闭发现设备            bluetoothAdapter.canceldiscovery();            try {                mSocket.connect();            } catch (IOException connectException) {                try {                    mSocket.close();                } catch (IOException closeException) {                    return;                }            }            Log.d("aaa", "connected");            // 自定义方法            manageConnectedSocket(mSocket);        }        private voID manageConnectedSocket(BluetoothSocket socket) {            //读取数据        }        public voID cancle() {            try {                mSocket.close();            } catch (IOException closeException) {            }        }    }

 

总结

以上是内存溢出为你收集整理的Android蓝牙 *** 作全部内容,希望文章能够帮你解决Android蓝牙 *** 作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1072370.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存