Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.invisiblewrench.fluttermidicommand

import android.bluetooth.BluetoothDevice

private val deviceServiceUUIDs = mutableMapOf<String, List<String>>()

var BluetoothDevice.serviceUUIDs: List<String>
get() = deviceServiceUUIDs[address] ?: listOf()
set(value) {
deviceServiceUUIDs[address] = value
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ abstract class Device {
lateinit var midiDevice: MidiDevice
protected var receiver:MidiReceiver? = null
protected var setupStreamHandler: FMCStreamHandler? = null
var serviceUUIDs: List<String> = listOf()

constructor(id: String, type: String) {
this.id = id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,9 @@ class FlutterMidiCommandPlugin : FlutterPlugin, ActivityAware, MethodCallHandler
Log.d("FlutterMIDICommand", "onScanResult: ${result?.device?.address} - ${result?.device?.name}")
result?.also {
if (!discoveredDevices.contains(it.device)) {
// Get and save serviceUUIDs
val serviceUUIDs = result.scanRecord?.serviceUuids?.map { it.uuid.toString() } ?: listOf()
it.device.serviceUUIDs = serviceUUIDs
discoveredDevices.add(it.device)
setupStreamHandler.send("deviceAppeared")
}
Expand Down Expand Up @@ -551,18 +554,23 @@ class FlutterMidiCommandPlugin : FlutterPlugin, ActivityAware, MethodCallHandler
var id = it.address;
Log.d("FlutterMIDICommand", "add discovered device $ type ${it.type}")

if (list.contains(id)) {
Log.d("FlutterMIDICommand", "device already in list $id")
} else {
if (!list.contains(id)) {
Log.d("FlutterMIDICommand", "add native device $id type ${it.type}")
list[id] = mapOf(
val deviceMap = mutableMapOf(
"name" to it.name,
"id" to id,
"type" to "BLE",
"connected" to if (connectedDevices.contains(id)) "true" else "false",
"inputs" to listOf(mapOf("id" to 0, "connected" to false)),
"outputs" to listOf(mapOf("id" to 0, "connected" to false))
)

// Only add to device information if serviceUUIDs is not empty
if (it.serviceUUIDs.isNotEmpty()) {
deviceMap["serviceUUIDs"] = it.serviceUUIDs
}

list[id] = deviceMap
}
}

Expand Down
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ class MyAppState extends State<MyApp> {
style: Theme.of(context).textTheme.headlineSmall,
),
subtitle: Text(
"ins:${device.inputPorts.length} outs:${device.outputPorts.length}, ${device.id}, ${device.type}"),
"ins:${device.inputPorts.length} outs:${device.outputPorts.length}, ${device.id}, ${device.type}, serviceUUID:${device.serviceUUIDs.first.str}"),
leading: Icon(device.connected
? Icons.radio_button_on
: Icons.radio_button_off),
Expand Down
44 changes: 34 additions & 10 deletions ios/Classes/SwiftFlutterMidiCommandPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public class SwiftFlutterMidiCommandPlugin: NSObject, CBCentralManagerDelegate,

let midiLog = OSLog(subsystem: "com.invisiblewrench.FlutterMidiCommand", category: "MIDI")


// Add a dictionary to store serviceUUIDs
private var peripheralServiceUUIDs: [CBPeripheral: [String]] = [:]

public static func register(with registrar: FlutterPluginRegistrar) {
#if os(macOS)
let channel = FlutterMethodChannel(name: "plugins.invisiblewrench.com/flutter_midi_command", binaryMessenger: registrar.messenger)
Expand Down Expand Up @@ -560,17 +564,28 @@ public class SwiftFlutterMidiCommandPlugin: NSObject, CBCentralManagerDelegate,

for periph:CBPeripheral in discoveredDevices {
let id = periph.identifier.uuidString
devices.append([
"name" : periph.name ?? "Unknown",
"id" : id,
"type" : "BLE",
"connected":(connectedDevices.keys.contains(id) ? "true" : "false"),
"inputs" : [["id":0, "connected":false] as [String:Any]],
"outputs" : [["id":0, "connected":false] as [String:Any]]
])
let serviceUUID = periph.discoverServices(nil)

// 获取存储的serviceUUIDs
let serviceUUIDs = peripheralServiceUUIDs[periph] ?? []

var deviceInfo: [String: Any] = [
"name": periph.name ?? "Unknown",
"id": id,
"type": "BLE",
"connected": (connectedDevices.keys.contains(id) ? "true" : "false"),
"inputs": [["id":0, "connected":false] as [String:Any]],
"outputs": [["id":0, "connected":false] as [String:Any]]
]

// 只有当serviceUUIDs不为空时才添加到设备信息中
if !serviceUUIDs.isEmpty {
deviceInfo["serviceUUIDs"] = serviceUUIDs
}

devices.append(deviceInfo)
}



// ###########
// CONNECTED BLE DEVICES (which are no longer discoverable)
// ###########
Expand Down Expand Up @@ -898,6 +913,15 @@ public class SwiftFlutterMidiCommandPlugin: NSObject, CBCentralManagerDelegate,
print("central didDiscover \(peripheral)")
if !discoveredDevices.contains(peripheral) {
discoveredDevices.insert(peripheral)

// Get serviceUUIDs from advertisementData
var serviceUUIDs: [String] = []
if let services = advertisementData[CBAdvertisementDataServiceUUIDsKey] as? [CBUUID] {
serviceUUIDs = services.map { $0.uuidString }
}

// Store serviceUUIDs in the peripheral's associated data
peripheralServiceUUIDs[peripheral] = serviceUUIDs
updateSetupState(data: "deviceAppeared")
}
}
Expand Down