Commit 787929e5 authored by Zaihang's avatar Zaihang

init

parents
.DS_Store
.dart_tool/
.packages
.pub/
build/
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<codeStyleSettings language="XML">
<indentOptions>
<option name="CONTINUATION_INDENT_SIZE" value="4" />
</indentOptions>
<arrangement>
<rules>
<section>
<rule>
<match>
<AND>
<NAME>xmlns:android</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>xmlns:.*</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*:id</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*:name</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>name</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>style</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
</AND>
</match>
<order>ANDROID_ATTRIBUTE_ORDER</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>.*</XML_NAMESPACE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
</rules>
</arrangement>
</codeStyleSettings>
</code_scheme>
</component>
\ No newline at end of file
<component name="libraryTable">
<library name="Dart SDK">
<CLASSES>
<root url="file:///home/zhou/Android/flutter/bin/cache/dart-sdk/lib/async" />
<root url="file:///home/zhou/Android/flutter/bin/cache/dart-sdk/lib/collection" />
<root url="file:///home/zhou/Android/flutter/bin/cache/dart-sdk/lib/convert" />
<root url="file:///home/zhou/Android/flutter/bin/cache/dart-sdk/lib/core" />
<root url="file:///home/zhou/Android/flutter/bin/cache/dart-sdk/lib/developer" />
<root url="file:///home/zhou/Android/flutter/bin/cache/dart-sdk/lib/html" />
<root url="file:///home/zhou/Android/flutter/bin/cache/dart-sdk/lib/io" />
<root url="file:///home/zhou/Android/flutter/bin/cache/dart-sdk/lib/isolate" />
<root url="file:///home/zhou/Android/flutter/bin/cache/dart-sdk/lib/math" />
<root url="file:///home/zhou/Android/flutter/bin/cache/dart-sdk/lib/mirrors" />
<root url="file:///home/zhou/Android/flutter/bin/cache/dart-sdk/lib/typed_data" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<library name="Flutter Plugins" type="FlutterPluginsLibraryType">
<CLASSES>
<root url="file://$PROJECT_DIR$" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<library name="Flutter for Android">
<CLASSES>
<root url="jar:///home/zhou/Android/flutter/bin/cache/artifacts/engine/android-arm/flutter.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectType">
<option name="id" value="io.flutter" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/flutter_bt_bluetooth.iml" filepath="$PROJECT_DIR$/flutter_bt_bluetooth.iml" />
<module fileurl="file://$PROJECT_DIR$/android/flutter_bt_bluetooth_android.iml" filepath="$PROJECT_DIR$/android/flutter_bt_bluetooth_android.iml" />
<module fileurl="file://$PROJECT_DIR$/example/android/flutter_bt_bluetooth_example_android.iml" filepath="$PROJECT_DIR$/example/android/flutter_bt_bluetooth_example_android.iml" />
</modules>
</component>
</project>
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="example/lib/main.dart" type="FlutterRunConfigurationType" factoryName="Flutter" singleton="false">
<option name="filePath" value="$PROJECT_DIR$/example/lib/main.dart" />
<method v="2" />
</configuration>
</component>
\ No newline at end of file
This diff is collapsed.
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.
version:
revision: 27321ebbad34b0a3fafe99fac037102196d655ff
channel: stable
project_type: plugin
## 0.0.1
* TODO: Describe initial release.
TODO: Add your license here.
# flutter_bt_bluetooth
A Flutter BT BlueTooth plugin.
## Getting Started
This project is a starting point for a Flutter
[plug-in package](https://flutter.dev/developing-packages/),
a specialized package that includes platform-specific implementation code for
Android and/or iOS.
For help getting started with Flutter, view our
[online documentation](https://flutter.dev/docs), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
group 'xyz.zhzh.flutter_bt_bluetooth'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
rootProject.allprojects {
repositories {
google()
jcenter()
}
}
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 29
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
defaultConfig {
minSdkVersion 26
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
lintOptions {
disable 'InvalidPackage'
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
package android
rootProject.name = 'flutter_bt_bluetooth'
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xyz.zhzh.flutter_bt_bluetooth">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
</manifest>
package xyz.zhzh.flutter_bt_bluetooth
internal object BluetoothState {
// Constants that indicate the current connection state
const val STATE_NONE = 0 // we're doing nothing
const val STATE_LISTEN = 1 // now listening for incoming connections
const val STATE_CONNECTING = 2 // now initiating an outgoing connection
const val STATE_CONNECTED = 3 // now connected to a remote device
const val STATE_NULL = -1 // now service is null
const val MESSAGE_STATE_CHANGE = 1
const val MESSAGE_READ = 2
const val MESSAGE_WRITE = 3
const val MESSAGE_DEVICE_NAME = 4
const val MESSAGE_TOAST = 5
const val REQUEST_CONNECT_DEVICE = 384
const val REQUEST_ENABLE_BT = 385
const val DEVICE_NAME = "device_name"
const val DEVICE_ADDRESS = "device_address"
const val TOAST = "toast"
const val DEVICE_ANDROID = true
const val DEVICE_OTHER = false
// Return Intent extra
var EXTRA_DEVICE_ADDRESS = "device_address"
}
\ No newline at end of file
package xyz.zhzh.flutter_bt_bluetooth
//import android.app.Activity
//import android.app.Application
//import android.os.Bundle
//import android.view.View
//import io.flutter.plugin.common.MethodCall
//import io.flutter.plugin.common.MethodChannel
//import io.flutter.plugin.common.PluginRegistry
//import io.flutter.plugin.platform.PlatformView
//
//class BtVideoView(private val registrar: PluginRegistry.Registrar, id: Int) :
// PlatformView, MethodChannel.MethodCallHandler {
//
// init {
//
// registrar.activity().application.registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks {
// override fun onActivityPaused(p0: Activity?) {
// }
//
// override fun onActivityResumed(p0: Activity?) {
// }
//
// override fun onActivityStarted(p0: Activity?) {
// }
//
// override fun onActivityDestroyed(p0: Activity?) {
// }
//
// override fun onActivitySaveInstanceState(p0: Activity?, p1: Bundle?) {
// }
//
// override fun onActivityStopped(p0: Activity?) {
// }
//
// override fun onActivityCreated(p0: Activity?, p1: Bundle?) {
// }
// })
// }
//
// override fun getView(): View {
// //TODO: not impl
// }
//
// override fun dispose() {
//
// }
//
// override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
//
// }
//
//}
package xyz.zhzh.flutter_bt_bluetooth
//import android.content.Context
//import io.flutter.plugin.common.PluginRegistry
//import io.flutter.plugin.common.StandardMessageCodec
//import io.flutter.plugin.platform.PlatformView
//import io.flutter.plugin.platform.PlatformViewFactory
//
//
//class BtVideoViewFactory(private val registrar: PluginRegistry.Registrar) :
// PlatformViewFactory(StandardMessageCodec.INSTANCE) {
//
// override fun create(context: Context, id: Int, obj: Any?): PlatformView {
// return BtVideoView(registrar,id)
// }
//
//}
\ No newline at end of file
package xyz.zhzh.flutter_bt_bluetooth
import android.Manifest
import android.app.Activity
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.content.Context
import android.content.pm.PackageManager
import android.widget.Toast
import androidx.annotation.NonNull
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.EventChannel.EventSink
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar
import io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener
/** FlutterBtBluetoothPlugin */
class FlutterBtBluetoothPlugin(r: Registrar) : MethodCallHandler, RequestPermissionsResultListener {
private val activity: Activity = r.activity()
private val channel: MethodChannel = MethodChannel(r.messenger(), NAMESPACE)
private var eventChannel: EventChannel? = null
private val mBluetoothManager: BluetoothManager = activity.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
private val mBluetoothAdapter: BluetoothAdapter = this.mBluetoothManager.adapter
private val mBluetoothUtil: BluetoothUtil = BluetoothUtil(r.context())
// Pending result for getBondedDevices, in the case where permissions are needed
private var pendingResult: Result? = null
companion object {
private const val NAMESPACE = "plugins.zhzh.xyz/flutter_bt_bluetooth"
private const val REQUEST_COARSE_LOCATION_PERMISSIONS = 1452
@JvmStatic
fun registerWith(registrar: Registrar) {
val instance = FlutterBtBluetoothPlugin(registrar)
registrar.addRequestPermissionsResultListener(instance)
// registrar.platformViewRegistry().registerViewFactory("$NAMESPACE/video", )
}
}
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
when (call.method) {
"getPlatformVersion" -> {
result.success("Android ${android.os.Build.VERSION.RELEASE}")
}
"getBondedDevices" -> {
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
pendingResult = result
ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION), REQUEST_COARSE_LOCATION_PERMISSIONS)
}
getBondedDevices(result)
}
"connectBondedDevices" -> {
val deviceId: String = call.arguments as String
mBluetoothUtil.setBluetoothConnectionListener(null)
mBluetoothUtil.setOnDataReceivedListener(null)
mBluetoothUtil.connect(deviceId)
result.success(null)
}
else -> {
result.notImplemented()
}
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>?, grantResults: IntArray?): Boolean {
if (requestCode == REQUEST_COARSE_LOCATION_PERMISSIONS) {
if (grantResults!![0] == PackageManager.PERMISSION_GRANTED) {
getBondedDevices(pendingResult!!)
} else {
pendingResult!!.error(
"no_permissions", "flutter_blue plugin requires location permissions for scanning", null)
pendingResult = null
}
return true
}
return false
}
init {
if (!mBluetoothUtil.isServiceAvailable) {
mBluetoothUtil.setupService()
mBluetoothUtil.startService(BluetoothState.DEVICE_ANDROID)
}
this.channel.setMethodCallHandler(this)
eventChannel = EventChannel(r.messenger(), "$NAMESPACE/output")
eventChannel!!.setStreamHandler(this.bluetoothOutputStreamHandler())
}
private fun getBondedDevices(result: Result) {
try {
val devices = mBluetoothAdapter.bondedDevices
val map: MutableMap<String, String> = HashMap()
for (d in devices) {
var name: String? = d.name
if (name == null) name = "UNKNOWN"
map[d.address!!] = name
}
result.success(map)
} catch (e: Exception) {
result.error("getBondedDevicesFair", e.message, e)
}
}
private fun bluetoothOutputStreamHandler(): EventChannel.StreamHandler {
return object : EventChannel.StreamHandler {
private var sink: EventSink? = null
private fun init() {
mBluetoothUtil.setOnDataReceivedListener(object : BluetoothUtil.OnDataReceivedListener {
override fun onDataReceived(data: ByteArray?, message: String?) {
if (data != null) {
when (message) {
"text" -> {
}
"photo" -> {
}
"video" -> {
}
else -> {
}
}
// mBluetoothUtil.send()
sink!!.success(data)
}
}
})
mBluetoothUtil.setBluetoothConnectionListener(object : BluetoothUtil.BluetoothConnectionListener {
override fun onDeviceConnected(name: String?, address: String?) {
Toast.makeText(activity, "蓝牙已连接", Toast.LENGTH_SHORT).show()
}
override fun onDeviceDisconnected() {
Toast.makeText(activity, "蓝牙已断开", Toast.LENGTH_SHORT).show()
}
override fun onDeviceConnectionFailed() {
Toast.makeText(activity, "蓝牙连接失败, 请检查蓝牙设置", Toast.LENGTH_SHORT).show()
}
})
}
override fun onListen(arguments: Any?, eventSink: EventSink) {
sink = eventSink
init()
}
override fun onCancel(arguments: Any?) {
mBluetoothUtil.setOnDataReceivedListener(null)
mBluetoothUtil.setBluetoothConnectionListener(null)
sink = null
}
}
}
}
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
# IntelliJ related
*.iml
*.ipr
*.iws
.idea/
# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/
# Flutter/Dart/Pub related
**/doc/api/
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
/build/
# Web related
lib/generated_plugin_registrant.dart
# Exceptions to above rules.
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.
version:
revision: 27321ebbad34b0a3fafe99fac037102196d655ff
channel: stable
project_type: app
# flutter_bt_bluetooth_example
Demonstrates how to use the flutter_bt_bluetooth plugin.
## Getting Started
This project is a starting point for a Flutter application.
A few resources to get you started if this is your first Flutter project:
- [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab)
- [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook)
For help getting started with Flutter, view our
[online documentation](https://flutter.dev/docs), which offers tutorials,
samples, guidance on mobile development, and a full API reference.