当前位置: 首页 > wzjs >正文

视频剪辑自学网站wordpress digg

视频剪辑自学网站,wordpress digg,西部数码网站管理助手2,三网站建设Android 应用蓝牙连接通信实现,主要包括如下步骤: 一.打开蓝牙 // 获取蓝牙适配器 BluetoothAdapter bluetoothAdapter BluetoothAdapter.getDefaultAdapter() 1.判断蓝牙是否打开, bluetoothAdapter.isEnabled() 2. 如果未打开,执行打开蓝牙…

Android 应用蓝牙连接通信实现,主要包括如下步骤:


一.打开蓝牙


// 获取蓝牙适配器
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
1.判断蓝牙是否打开,
bluetoothAdapter.isEnabled()
2. 如果未打开,执行打开蓝牙,需要权限:Manifest.permission.BLUETOOTH
bluetoothAdapter.enable()
或者打开系统蓝牙设置界面让用户点击打开蓝牙。
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);


二.搜索蓝牙设备列表,配对需要连接的设备


1.启动设备发现
bluetoothAdapter.startDiscovery()
2.注册广播监听发现到的蓝牙设备
如果发现蓝牙设备可以获取BluetoothDevice对象

// 注册广播接收器监听设备发现
private final BroadcastReceiver findDeviceReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();if (BluetoothDevice.ACTION_FOUND.equals(action)) {// 发现新设备BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {return;}Log.d("Bluetooth", "发现设备: " + device.getName() + " - " + device.getAddress());// 添加到设备列表(如 RecyclerView)}}
};
// 注册广播监听附近蓝牙设备
//IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
//registerReceiver(findDeviceReceiver, filter);


3.配对
3.1 客户端设备(主动发起连接的设备)发起配对(系统自动处理弹窗)
    device.createBond();    
    手机A系统弹窗显示配对码待用户点击确定,
3.2  服务端设备(等待连接的设备)处理配对
发起配对后,同时待连接蓝牙设备也会弹窗显示配对码待用户点击确定。
发起配对端和待连接蓝牙设备在配对界面点击确定后完成配对。如果超时未点击确定,会弹窗超时提醒。
这里可以 注册广播接收器监听ACTION_PAIRING_REQUEST来监听请求配对信息,该广播会在蓝牙配对时触发并携带设备信息。
ps:一般Android系统已经做了广播监听处理配对请求,无需用户自定义。

//广播监听蓝牙配对请求
BroadcastReceiver pairRequestReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(intent.getAction())) {BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);int pairingVariant = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, -1);if (pairingVariant == BluetoothDevice.PAIRING_VARIANT_PIN) {// 处理PIN码请求,可以调用startSystemPairDialog打开系统蓝牙配对弹窗// 或者调用startUserPairDialog打开用户自定义蓝牙配对弹窗}}}
};
// 注册广播监听配对请求
//IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
//registerReceiver(pairRequestReceiver, filter);/*** 打开系统蓝牙配对弹窗* @param context* @param device 通过action ACTION_PAIRING_REQUEST 获取到的请求配对的BluetoothDevice.*/
private void startSystemPairDialog(Context context, BluetoothDevice device) {Intent pairingIntent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST);pairingIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);pairingIntent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.PAIRING_VARIANT_PIN);pairingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {return;}context.startActivity(pairingIntent);
}/*** 打开用户自定义蓝牙配对弹窗,点击确定完成配对请求。* @param context* @param device*/
private void startUserPairDialog(Context context, BluetoothDevice device) {//todo 自定义弹窗 点击确定, 执行如下 调用setPin处理PIN码配对try {Method setPin = device.getClass().getMethod("setPin", byte[].class);setPin.invoke(device, "1234".getBytes()); // 替换为设备预设的PIN码Method createBond = device.getClass().getMethod("createBond");createBond.invoke(device);} catch (Exception e) {e.printStackTrace();}
}


3.3 如果是已经配对过,可获取已配对设备列表

Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
for (BluetoothDevice device : pairedDevices) {String macAddress = device.getAddress(); // 获取已连接设备的MAC地址
}


三. 连接设备,数据通信


1. 客户端设备(主动发起连接的设备)创建RFCOMM Socket并连接 连接远程设备
//配对过后,创建RFCOMM Socket并连接到服务:使用UUID指定服务标识符,创建RFCOMM Socket并连接到远程设备。

/*** 客户端设备(主动发起连接的设备)连接服务端获取 BluetoothSocket 和服务端socket数据通信** @param context* @param device* @throws IOException*/
private void clientConnect(Context context, BluetoothDevice device) throws IOException {if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {return;}BluetoothSocket socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));socket.connect(); // 阻塞式连接//连接之后就可以获取输入输出流进行 读写操作了InputStream inputStream = socket.getInputStream();OutputStream outputStream = socket.getOutputStream();
}

2.服务端设备(等待连接的设备)监听连接(BluetoothServerSocket)
连接之后就可以基于socket获取输入输出流进行 读写操作了。    

/*** 服务端设备(等待连接的设备)连接客户端后 获取BluetoothServerSocket 和客户端socket数据通信** @param context* @param device* @throws IOException*/
private void serverListen(Context context, BluetoothDevice device) throws IOException {BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();//name:字符串参数,表示服务的名称,这个名称会被写入设备的SDP(服务发现协议)数据库。//uuid:UUID,用于标识服务,客户端和服务器必须使用相同的UUID才能建立连接。if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {return;}BluetoothServerSocket serverSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord("MyBluetoothApp", UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));BluetoothSocket socket;while (true) {try {socket = serverSocket.accept(); // 阻塞等待客户端连接if (socket != null) {// 连接成功,启动数据传输线程//客户端连接之后就可以获取输入输出流进行 读写操作了InputStream inputStream = socket.getInputStream();OutputStream outputStream = socket.getOutputStream();serverSocket.close();break;}} catch (IOException e) {Log.e("Bluetooth", "ServerSocket异常", e);break;}}
}


    


文章转载自:

http://DdWrueWA.wfLpj.cn
http://6ha0VyP6.wfLpj.cn
http://bQFd9BQC.wfLpj.cn
http://5lEKwqbP.wfLpj.cn
http://Cr0Hmj34.wfLpj.cn
http://mV95Ymtv.wfLpj.cn
http://rsyXv4bV.wfLpj.cn
http://ijetajaa.wfLpj.cn
http://atbhkd9r.wfLpj.cn
http://zaBIpOZI.wfLpj.cn
http://6v7X0cOF.wfLpj.cn
http://bt73SFbL.wfLpj.cn
http://myS7XQrX.wfLpj.cn
http://rxP3qIG0.wfLpj.cn
http://tiKh3sOK.wfLpj.cn
http://gJePEKje.wfLpj.cn
http://2RJ0cOM9.wfLpj.cn
http://9WADixMj.wfLpj.cn
http://YgePY5OR.wfLpj.cn
http://I1mzJHlF.wfLpj.cn
http://H9s5OEXD.wfLpj.cn
http://nySNBHsQ.wfLpj.cn
http://kv1TtHvD.wfLpj.cn
http://TTJlcq3A.wfLpj.cn
http://24cUU7yg.wfLpj.cn
http://ZQevRPt4.wfLpj.cn
http://j9hGFphV.wfLpj.cn
http://MnboEMZm.wfLpj.cn
http://Gz3avJRt.wfLpj.cn
http://SEFedezX.wfLpj.cn
http://www.dtcms.com/wzjs/740391.html

相关文章:

  • 三亚做网站济南营销型网站建设贵吗
  • 建企业门户网站广州科 外贸网站建设
  • 做的最好的网站公司做外贸在哪个网站
  • 百度云服务器做asp网站杭州seo教程
  • 网站设计编程有哪些同声传译公司网站建设
  • 专业电商网站济宁网站开发平台
  • ASP个人网站的建设做网站有前途
  • 计算机网站建设招聘怎么查网站的关键词排名
  • 手机主页网站推荐湖南网站优化代运营
  • 爱最好网站建设设计在线中国
  • 网站设计与建设的农夫山泉软文300字
  • 百度收录比较好的网站网站建设全包需要多少钱
  • 番禺网站优化平台以蓝色为主色调的网站
  • 网站用什么语言好安阳县面积
  • 做音乐网站要多少钱品牌网站建设案例
  • wordpress博客文章怎么设置百度seo按天计费
  • 宁波网站建设信息网站优化排名分享隐迅推
  • 湖州市住房和城乡建设局网站网站开发答辩记录表
  • 遂宁市网站建设滨城网站开发
  • 胶州市 网站建设企业如何创建品牌
  • 全屏产品网站专业网站建设费用
  • 济南网站怎么做seo做配件出口上什么网站
  • 青海汽车网站建设推广普通话手抄报简单又好看内容
  • iis6 建设网站浏览模板做的网站如何下载
  • 网站建设视频技术论坛阿里云域名怎么做网站
  • 如何用子域名做网站wordpress主题放哪
  • 如果让你建设一个网站上海设计网站公司
  • 用php做网站要用什么软件曲阜做网站哪家好
  • 电子商务网站的建设与规划书软件界面设计方案
  • 手机网站底部固定菜单wordpress登陆不跳转