瀏覽代碼

升级1.0.1
完善了Socket状态 Read Close

Kevin 5 年之前
父節點
當前提交
22f5bd0494

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
+  </component>
+</project>

+ 69 - 62
.idea/workspace.xml

@@ -1,7 +1,13 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
   <component name="ChangeListManager">
-    <list default="true" id="cef19225-ccb5-4e06-af53-e383e247fd01" name="Default Changelist" comment="" />
+    <list default="true" id="cef19225-ccb5-4e06-af53-e383e247fd01" name="Default Changelist" comment="">
+      <change beforePath="$PROJECT_DIR$/android/src/main/java/info/geteasy/flutter_bluetooth/ConnectedHandler.java" beforeDir="false" afterPath="$PROJECT_DIR$/android/src/main/java/info/geteasy/flutter_bluetooth/ConnectedHandler.java" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/android/src/main/java/info/geteasy/flutter_bluetooth/ConnectedThread.java" beforeDir="false" afterPath="$PROJECT_DIR$/android/src/main/java/info/geteasy/flutter_bluetooth/ConnectedThread.java" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/example/lib/main.dart" beforeDir="false" afterPath="$PROJECT_DIR$/example/lib/main.dart" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/example/pubspec.yaml" beforeDir="false" afterPath="$PROJECT_DIR$/example/pubspec.yaml" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/lib/bluetooth_socket.dart" beforeDir="false" afterPath="$PROJECT_DIR$/lib/bluetooth_socket.dart" afterDir="false" />
+    </list>
     <ignored path="$PROJECT_DIR$/.dart_tool/" />
     <ignored path="$PROJECT_DIR$/.idea/" />
     <ignored path="$PROJECT_DIR$/.pub/" />
@@ -14,7 +20,7 @@
     <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
     <option name="LAST_RESOLUTION" value="IGNORE" />
   </component>
-  <component name="ExecutionTargetManager" SELECTED_TARGET="69DDU16528001447" />
+  <component name="ExecutionTargetManager" SELECTED_TARGET="192.168.0.12:5555" />
   <component name="FileEditorManager">
     <leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
       <file pinned="false" current-in-tab="false">
@@ -26,7 +32,7 @@
         <entry file="file://$PROJECT_DIR$/android/src/main/AndroidManifest.xml">
           <provider selected="true" editor-type-id="text-editor">
             <state relative-caret-position="19">
-              <caret line="1" column="43" lean-forward="true" selection-start-line="1" selection-start-column="43" selection-end-line="4" selection-end-column="78" />
+              <caret line="1" column="43" selection-start-line="1" selection-start-column="43" selection-end-line="4" selection-end-column="78" />
             </state>
           </provider>
         </entry>
@@ -34,17 +40,15 @@
       <file pinned="false" current-in-tab="true">
         <entry file="file://$PROJECT_DIR$/android/src/main/java/info/geteasy/flutter_bluetooth/FlutterBluetoothPlugin.java">
           <provider selected="true" editor-type-id="text-editor">
-            <state relative-caret-position="-368">
-              <caret line="15" column="13" selection-start-line="15" selection-start-column="13" selection-end-line="15" selection-end-column="13" />
+            <state relative-caret-position="-912">
+              <caret line="26" column="46" selection-start-line="26" selection-start-column="46" selection-end-line="26" selection-end-column="46" />
             </state>
           </provider>
         </entry>
       </file>
       <file pinned="false" current-in-tab="false">
         <entry file="file://$PROJECT_DIR$/example/lib/main.dart">
-          <provider selected="true" editor-type-id="text-editor">
-            <state relative-caret-position="-497" />
-          </provider>
+          <provider selected="true" editor-type-id="text-editor" />
         </entry>
       </file>
     </leaf>
@@ -54,6 +58,9 @@
       <find>android.permission.BLUETOOTH</find>
     </findStrings>
   </component>
+  <component name="Git.Settings">
+    <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
+  </component>
   <component name="IdeDocumentHistory">
     <option name="CHANGED_PATHS">
       <list>
@@ -63,63 +70,65 @@
     </option>
   </component>
   <component name="ProjectFrameBounds" extendedState="6">
-    <option name="x" value="-8" />
+    <option name="x" value="1912" />
     <option name="y" value="-8" />
     <option name="width" value="1936" />
     <option name="height" value="1056" />
   </component>
+  <component name="ProjectLevelVcsManager" settingsEditedManually="true" />
   <component name="ProjectView">
     <navigator currentView="ProjectPane" proportions="" version="1">
       <foldersAlwaysOnTop value="true" />
     </navigator>
     <panes>
+      <pane id="Scope" />
       <pane id="AndroidView" />
-      <pane id="ProjectPane">
+      <pane id="PackagesPane">
         <subPane>
           <expand>
             <path>
-              <item name="flutter_bluetooth" type="b2602c69:ProjectViewProjectNode" />
-              <item name="flutter_bluetooth" type="462c0819:PsiDirectoryNode" />
+              <item name="flutter_bluetooth" type="58d84e3e:PackageViewProjectNode" />
+              <item name="flutter_bluetooth" type="616d4139:PackageViewModuleNode" />
             </path>
             <path>
-              <item name="flutter_bluetooth" type="b2602c69:ProjectViewProjectNode" />
-              <item name="flutter_bluetooth" type="462c0819:PsiDirectoryNode" />
-              <item name="android" type="462c0819:PsiDirectoryNode" />
+              <item name="flutter_bluetooth" type="58d84e3e:PackageViewProjectNode" />
+              <item name="flutter_bluetooth_android" type="616d4139:PackageViewModuleNode" />
             </path>
             <path>
-              <item name="flutter_bluetooth" type="b2602c69:ProjectViewProjectNode" />
-              <item name="flutter_bluetooth" type="462c0819:PsiDirectoryNode" />
-              <item name="android" type="462c0819:PsiDirectoryNode" />
-              <item name="main" type="462c0819:PsiDirectoryNode" />
+              <item name="flutter_bluetooth" type="58d84e3e:PackageViewProjectNode" />
+              <item name="flutter_bluetooth_android" type="616d4139:PackageViewModuleNode" />
+              <item name="info.geteasy.flutter_bluetooth" type="1f31426a:PackageElementNode" />
             </path>
           </expand>
           <select />
         </subPane>
       </pane>
-      <pane id="PackagesPane">
+      <pane id="ProjectPane">
         <subPane>
           <expand>
             <path>
-              <item name="flutter_bluetooth" type="58d84e3e:PackageViewProjectNode" />
-              <item name="flutter_bluetooth" type="616d4139:PackageViewModuleNode" />
+              <item name="flutter_bluetooth" type="b2602c69:ProjectViewProjectNode" />
+              <item name="flutter_bluetooth" type="462c0819:PsiDirectoryNode" />
             </path>
             <path>
-              <item name="flutter_bluetooth" type="58d84e3e:PackageViewProjectNode" />
-              <item name="flutter_bluetooth_android" type="616d4139:PackageViewModuleNode" />
+              <item name="flutter_bluetooth" type="b2602c69:ProjectViewProjectNode" />
+              <item name="flutter_bluetooth" type="462c0819:PsiDirectoryNode" />
+              <item name="android" type="462c0819:PsiDirectoryNode" />
             </path>
             <path>
-              <item name="flutter_bluetooth" type="58d84e3e:PackageViewProjectNode" />
-              <item name="flutter_bluetooth_android" type="616d4139:PackageViewModuleNode" />
-              <item name="info.geteasy.flutter_bluetooth" type="1f31426a:PackageElementNode" />
+              <item name="flutter_bluetooth" type="b2602c69:ProjectViewProjectNode" />
+              <item name="flutter_bluetooth" type="462c0819:PsiDirectoryNode" />
+              <item name="android" type="462c0819:PsiDirectoryNode" />
+              <item name="main" type="462c0819:PsiDirectoryNode" />
             </path>
           </expand>
           <select />
         </subPane>
       </pane>
-      <pane id="Scope" />
     </panes>
   </component>
   <component name="PropertiesComponent">
+    <property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
     <property name="dart.analysis.tool.window.force.activate" value="false" />
     <property name="last_opened_file_path" value="$PROJECT_DIR$" />
     <property name="show.migrate.to.gradle.popup" value="false" />
@@ -150,56 +159,54 @@
     <servers />
   </component>
   <component name="ToolWindowManager">
-    <frame x="-8" y="-8" width="1936" height="1056" extended-state="6" />
+    <frame x="1912" y="-8" width="1936" height="1056" extended-state="6" />
     <editor active="true" />
     <layout>
-      <window_info id="Capture Tool" />
-      <window_info id="Structure" side_tool="true" />
-      <window_info id="Favorites" side_tool="true" />
-      <window_info id="Build Variants" side_tool="true" />
-      <window_info id="Image Layers" />
-      <window_info id="Designer" />
-      <window_info id="Captures" side_tool="true" />
-      <window_info id="Resources Explorer" />
       <window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.25586355" />
-      <window_info anchor="bottom" id="Run" />
-      <window_info anchor="bottom" id="Dart Analysis" visible="true" weight="0.33694476" />
-      <window_info anchor="bottom" id="Logcat" />
-      <window_info anchor="bottom" id="TODO" />
-      <window_info anchor="bottom" id="Debug" />
-      <window_info anchor="bottom" id="Terminal" />
-      <window_info anchor="bottom" id="Event Log" side_tool="true" />
-      <window_info anchor="bottom" id="Flutter Performance" side_tool="true" />
-      <window_info anchor="bottom" id="Version Control" />
-      <window_info anchor="bottom" id="Android Profiler" show_stripe_button="false" />
-      <window_info anchor="right" id="Device File Explorer" side_tool="true" />
-      <window_info anchor="right" id="Capture Analysis" />
-      <window_info anchor="right" id="Theme Preview" />
-      <window_info anchor="right" id="Flutter Inspector" />
-      <window_info anchor="right" id="Flutter Outline" />
-      <window_info anchor="right" id="Palette&#9;" />
+      <window_info id="Captures" order="1" side_tool="true" />
+      <window_info id="Structure" order="2" side_tool="true" />
+      <window_info id="Image Layers" order="3" />
+      <window_info id="Designer" order="4" />
+      <window_info id="Build Variants" order="5" side_tool="true" />
+      <window_info id="Resources Explorer" order="6" />
+      <window_info id="Capture Tool" order="7" />
+      <window_info id="Favorites" order="8" side_tool="true" />
+      <window_info anchor="bottom" id="Dart Analysis" order="0" visible="true" weight="0.3373102" />
+      <window_info anchor="bottom" id="Run" order="1" />
+      <window_info anchor="bottom" id="TODO" order="2" />
+      <window_info anchor="bottom" id="Android Profiler" order="3" show_stripe_button="false" />
+      <window_info anchor="bottom" id="Logcat" order="4" />
+      <window_info anchor="bottom" id="Debug" order="5" />
+      <window_info anchor="bottom" id="Terminal" order="6" />
+      <window_info anchor="bottom" id="Event Log" order="7" side_tool="true" />
+      <window_info anchor="bottom" id="Flutter Performance" order="8" side_tool="true" />
+      <window_info anchor="bottom" id="Version Control" order="9" />
+      <window_info anchor="right" id="Device File Explorer" order="0" side_tool="true" />
+      <window_info anchor="right" id="Capture Analysis" order="1" />
+      <window_info anchor="right" id="Theme Preview" order="2" />
+      <window_info anchor="right" id="Flutter Inspector" order="3" />
+      <window_info anchor="right" id="Flutter Outline" order="4" />
+      <window_info anchor="right" id="Palette&#9;" order="5" />
     </layout>
   </component>
   <component name="editorHistoryManager">
+    <entry file="file://$PROJECT_DIR$/lib/flutter_bluetooth.dart">
+      <provider selected="true" editor-type-id="text-editor" />
+    </entry>
     <entry file="file://$PROJECT_DIR$/android/src/main/AndroidManifest.xml">
       <provider selected="true" editor-type-id="text-editor">
         <state relative-caret-position="19">
-          <caret line="1" column="43" lean-forward="true" selection-start-line="1" selection-start-column="43" selection-end-line="4" selection-end-column="78" />
+          <caret line="1" column="43" selection-start-line="1" selection-start-column="43" selection-end-line="4" selection-end-column="78" />
         </state>
       </provider>
     </entry>
-    <entry file="file://$PROJECT_DIR$/lib/flutter_bluetooth.dart">
-      <provider selected="true" editor-type-id="text-editor" />
-    </entry>
     <entry file="file://$PROJECT_DIR$/example/lib/main.dart">
-      <provider selected="true" editor-type-id="text-editor">
-        <state relative-caret-position="-497" />
-      </provider>
+      <provider selected="true" editor-type-id="text-editor" />
     </entry>
     <entry file="file://$PROJECT_DIR$/android/src/main/java/info/geteasy/flutter_bluetooth/FlutterBluetoothPlugin.java">
       <provider selected="true" editor-type-id="text-editor">
-        <state relative-caret-position="-368">
-          <caret line="15" column="13" selection-start-line="15" selection-start-column="13" selection-end-line="15" selection-end-column="13" />
+        <state relative-caret-position="-912">
+          <caret line="26" column="46" selection-start-line="26" selection-start-column="46" selection-end-line="26" selection-end-column="46" />
         </state>
       </provider>
     </entry>

+ 11 - 4
android/src/main/java/info/geteasy/flutter_bluetooth/ConnectedHandler.java

@@ -41,14 +41,14 @@ public class ConnectedHandler extends Handler {
     public void handleMessage(Message msg) {
         super.handleMessage(msg);
 
+        Map<String, Object> result = new HashMap<>();
         switch (msg.what){
             case ConnectedThread.MESSAGE_READ://读取到数据
-                Map<String, Object> result = new HashMap<>();
                 result.put("type", ConnectedThread.MESSAGE_READ);
                 result.put("data", msg.obj);
-                result.put("length", msg.arg1);
-
-                mEventSink.success(result);
+                if(mEventSink != null){
+                    mEventSink.success(result);
+                }
                 break;
             case ConnectedThread.MESSAGE_SUCCESS: //连接成功
                 mConnectResult.success(null);
@@ -56,6 +56,13 @@ public class ConnectedHandler extends Handler {
             case ConnectedThread.MESSAGE_EXCEPTIONS: // 连接失败
                 mConnectResult.error(null, null, null);
                 break;
+            case ConnectedThread.MESSAGE_CLOSE:
+                result.put("type", ConnectedThread.MESSAGE_CLOSE);
+
+                if(mEventSink != null){
+                    mEventSink.success(result);
+                }
+                break;
         }
     }
 

+ 22 - 4
android/src/main/java/info/geteasy/flutter_bluetooth/ConnectedThread.java

@@ -3,9 +3,15 @@ package info.geteasy.flutter_bluetooth;
 import android.bluetooth.BluetoothSocket;
 import android.os.Handler;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.lang.reflect.Array;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
 
 public class ConnectedThread extends Thread {
     private final BluetoothSocket mSocket;
@@ -15,6 +21,7 @@ public class ConnectedThread extends Thread {
     public final static int MESSAGE_READ = 1;
     public final static int MESSAGE_EXCEPTIONS = 2;
     public final static int MESSAGE_SUCCESS = 3;
+    public final static int MESSAGE_CLOSE = 4;
 
     public ConnectedThread(BluetoothSocket socket, Handler handler) {
         mSocket = socket;
@@ -44,16 +51,27 @@ public class ConnectedThread extends Thread {
             } catch (IOException e1) {
                 e1.printStackTrace();
             }
+            return;
         }
-        int bytes;
+
         //对输入流保持监听直至一个异常发生
         while (true) {
-            byte[] buffer = new byte[20];
             try {
-                bytes = mInStream.read(buffer);
-                mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
+                ArrayList<Byte> buffer = new ArrayList<>();
+                int c = mInStream.read();
+                while (c != -1) {
+                    buffer.add((byte)c);
+                    c = mInStream.read();
+                }
+                mHandler.obtainMessage(MESSAGE_READ, -1, -1, buffer.toArray())
                         .sendToTarget();
             } catch (IOException e) {
+                try {
+                    mSocket.close();
+                } catch (IOException e1) {
+                    e1.printStackTrace();
+                }
+                mHandler.sendEmptyMessage(MESSAGE_CLOSE);
                 break;
             }
         }

+ 1 - 1
example/lib/main.dart

@@ -39,7 +39,7 @@ class _MyAppState extends State<MyApp> {
                   onTap: () async {
                     var socket = await result.data[index].createSocket('00001101-0000-1000-8000-00805F9B34FB');
                     await socket.write(Uint8List.fromList([...'\x1BB'.codeUnits, 5, 9]));
-                    await socket.close();
+                    // await socket.close();
                   },
               );
             },

+ 1 - 1
example/pubspec.lock

@@ -75,7 +75,7 @@ packages:
       path: ".."
       relative: true
     source: path
-    version: "1.0.0"
+    version: "1.0.1"
   flutter_test:
     dependency: "direct dev"
     description: flutter

+ 1 - 1
example/pubspec.yaml

@@ -3,7 +3,7 @@ description: Demonstrates how to use the flutter_bluetooth plugin.
 publish_to: 'none'
 
 environment:
-  sdk: ">=2.1.0 <3.0.0"
+  sdk: ">=2.2.2 <3.0.0"
 
 dependencies:
   flutter:

+ 44 - 15
lib/bluetooth_socket.dart

@@ -1,28 +1,57 @@
 part of flutter_bluetooth;
 
-class BluetoothSocket{
+class BluetoothSocket {
+  static const int _MESSAGE_READ = 1;
+  static const int _MESSAGE_EXCEPTIONS = 2;
+  static const int _MESSAGE_SUCCESS = 3;
+  static const int _MESSAGE_CLOSE = 4;
+
   final String _address;
   String get address => _address;
   final String _uuid;
   String get uuid => _uuid;
-  
 
-  BluetoothSocket._(this._address, this._uuid){
+  final StreamController<Uint8List> _readController =
+      StreamController<Uint8List>();
+  Stream<Uint8List> get readStream => _readController.stream;
+
+  final StreamController _closeController = StreamController();
+  Stream get closeStream => _closeController.stream;
+
+  BluetoothSocketStatusEnum _status = BluetoothSocketStatusEnum.Connected;
+  BluetoothSocketStatusEnum get status => _status;
+
+  BluetoothSocket._(this._address, this._uuid) {
+    this._handlerEvent();
   }
 
-  write(Uint8List data) async{
-    await FlutterBluetooth._channel.invokeMethod('write', {
-      'address': address,
-      'uuid': uuid,
-      'data': data
-    });
+  write(Uint8List data) async {
+    await FlutterBluetooth._channel.invokeMethod(
+        'write', {'address': address, 'uuid': uuid, 'data': data});
+  }
+
+  close() async {
+    await FlutterBluetooth._channel
+        .invokeMethod('close', {'address': address, 'uuid': uuid});
   }
 
-  close() async{
-    await FlutterBluetooth._channel.invokeMethod('close', {
-      'address': address,
-      'uuid': uuid
+  _handlerEvent() {
+    EventChannel('flutter_bluetooth/connectedEvent$address-$uuid')
+        .receiveBroadcastStream()
+        .takeUntil(closeStream)
+        .listen((event) {
+      switch (event['type']) {
+        case _MESSAGE_READ:
+          this._readController.add(event['data']);
+          break;
+        case _MESSAGE_CLOSE:
+          _status = BluetoothSocketStatusEnum.Disconnect;
+          _closeController.add(null);
+          break;
+        default:
+      }
     });
   }
-  
-}
+}
+
+enum BluetoothSocketStatusEnum { Connected, Disconnect }

+ 1 - 0
lib/flutter_bluetooth.dart

@@ -1,6 +1,7 @@
 library flutter_bluetooth;
 
 import 'dart:async';
+import 'dart:ffi';
 import 'dart:typed_data';
 
 import 'package:flutter/cupertino.dart';

+ 1 - 1
pubspec.yaml

@@ -1,6 +1,6 @@
 name: flutter_bluetooth
 description: 传统蓝牙
-version: 1.0.0
+version: 1.0.1
 author:
 homepage: