# ffmpeg_plugin **Repository Path**: Sun.start/ffmpeg_plugin ## Basic Information - **Project Name**: ffmpeg_plugin - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-10-16 - **Last Updated**: 2021-03-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 蓝牙通信 + [ButterKnife框架](https://blog.csdn.net/woaixuexihhh/article/details/90639970)采用得ButterKnife框架的使用 ##简介 - 首先再mainfests添加蓝牙权限和GPS定位权限(google在android6.0之后, 为了更好的保护用户的数据安全,所有需要访问硬件唯一标识符的地方都需要申请位置权限,需要动态申请我也木办法) ####开启蓝牙 - 然后再开启蓝牙如果没用启动蓝牙需要动态申请权限(也可以手动打开自己开心就好)然后判断```if (!bluetoothAdapter.isEnabled())```蓝牙是否开启如果没用开启就```bluetoothAdapter.enable()```一哈就好了 ####扫描蓝牙 - 然后就开始扫描了,不过开始之前需要注册搜索蓝牙设备广播如果没用注册搜索到只有mac地址了,搜索蓝牙广播```BluetoothDevice.ACTION_FOUND``` 之后才能开始扫描搜索 ```bluetoothAdapter.startDiscovery();``` ####配对蓝牙 - 搜索到的设备需要一个广播接收器来接收搜索到得数据 还要来判断当前接收到得广播是什么类型得```if (BluetoothDevice.ACTION_FOUND.equals(action))``` 就开始add新设备到list里面这时候你会发现列表数据有好多重复得这时候该怎么办呢 有Set再 这个可以去除重复得数据返回clear后列表 - 这时候拿到数据了此时需要连接他 可是怎么能把俩台没用关联设备连接呢 连接之前还是需要先配对。 - 这时候拿到 需要配对得```listBluetoothDevice.get(position) BluetoothDevice``` 这个类包含了Mac地址、设备名称、uuid等多个参数 ####连接蓝牙 - 如果配对成功后就开始连接蓝牙设备连接比较耗时需要在主线程再开启一个子线程连接操作再子线程中运行 ```javascript ThreadPoolProxyFactory.getNormalThreadPoolProxy().execute(new Runnable() { @Override public void run() { connect(listdevice.get(position)); } }); ``` - 连接成功后跳转到聊天页面文字通信 - 通信前需要开启一个服务端和接受消息服务端 ```javascript /** * 开启服务端 */ public void startBluService() { while (true) { try { if (getBluetoothServerSocket() == null){ Log.e("在这里获取的为空","在这里获取的为空"); return; } bluetoothSocket = getBluetoothServerSocket().accept(); if (bluetoothSocket != null) { APP.bluetoothSocket = bluetoothSocket; EventBus.getDefault().post(new BluRxBean(SERVER_ACCEPT,bluetoothSocket.getRemoteDevice())); //如果你的蓝牙设备只是一对一的连接,则执行以下代码 getBluetoothServerSocket().close(); //如果你的蓝牙设备是一对多的,则应该调用break;跳出循环 //break; } } catch (IOException e) { e.printStackTrace(); } } } /** * 接收消息服务端 */ public void receiveMessage(){ if (APP.bluetoothSocket == null){ return; } try { InputStream inputStream = APP.bluetoothSocket.getInputStream(); // 从客户端获取信息 BufferedReader bff = new BufferedReader(new InputStreamReader(inputStream)); String json; while (true) { while ((json = bff.readLine()) != null) { EventBus.getDefault().post(new MessageBean(RECEIVER_MESSAGE,json)); } if ("file".equals(json)) { FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory() +"/test.gif"); int length; int fileSzie = 0; byte[] b = new byte[1024]; // 2、把socket输入流写到文件输出流中去 while ((length = inputStream.read(b)) != -1) { fos.write(b, 0, length); fileSzie += length; System.out.println("当前大小:" + fileSzie); //这里通过先前传递过来的文件大小作为参照,因为该文件流不能自主停止,所以通过判断文件大小来跳出循环 } fos.close(); EventBus.getDefault().post(new MessageBean(RECEIVER_FILE,"文件保存成功")); } } } catch (IOException e) { e.printStackTrace(); } } ``` - 下面就可以操作通信了 ```javascript /** * 发送文本消息 * * @param message */ public static void sendMessage(String message) { if (APP.bluetoothSocket == null || TextUtils.isEmpty(message)) return; try { message += "\n"; OutputStream outputStream = APP.bluetoothSocket.getOutputStream(); outputStream.write(message.getBytes("utf-8")); outputStream.flush(); EventBus.getDefault().post(new MessageBean(BltContant.SEND_TEXT_SUCCESS)); } catch (IOException e) { e.printStackTrace(); } } ``` ```javascript /** * 发送文件 */ public static void sendMessageByFile(String filePath) { if (APP.bluetoothSocket == null || TextUtils.isEmpty(filePath)) return; try { OutputStream outputStream = APP.bluetoothSocket.getOutputStream(); //要传输的文件路径 File file = new File(filePath); //说明不存在该文件 if (!file.exists()){ EventBus.getDefault().post(new MessageBean(BltContant.SEND_FILE_NOTEXIT)); return; } //说明该文件是一个文件夹 if (file.isDirectory()) { EventBus.getDefault().post(new MessageBean(BltContant.SEND_FILE_IS_FOLDER)); return; } //1、发送文件信息实体类 outputStream.write("file".getBytes("utf-8")); //将文件写入流 FileInputStream fis = new FileInputStream(file); //每次上传1M的内容 byte[] b = new byte[1024]; int length; int fileSize = 0;//实时监测上传进度 while ((length = fis.read(b)) != -1) { fileSize += length; Log.i("socketChat", "文件上传进度:" + (fileSize / file.length() * 100) + "%"); //2、把文件写入socket输出流 outputStream.write(b, 0, length); } //关闭文件流 fis.close(); //该方法无效 //outputStream.write("\n".getBytes()); outputStream.flush(); EventBus.getDefault().post(new MessageBean(BltContant.SEND_FILE_SUCCESS)); } catch (IOException e) { e.printStackTrace(); } } ```