From a244f7da79e4585acbaca3bf1751a1ed059b03c6 Mon Sep 17 00:00:00 2001 From: Hugh Sanderson Date: Mon, 16 Sep 2024 14:49:59 +0800 Subject: [PATCH] Send bluetooth paired devices --- .../android/java/org/haxe/nme/Bluetooth.java | 58 +++++++++++-------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/templates/android/java/org/haxe/nme/Bluetooth.java b/templates/android/java/org/haxe/nme/Bluetooth.java index 9ddab7ceb..d1b255434 100644 --- a/templates/android/java/org/haxe/nme/Bluetooth.java +++ b/templates/android/java/org/haxe/nme/Bluetooth.java @@ -135,6 +135,29 @@ static void postDevices(final HaxeObject inHandler,final int inState, final Stri ); } + static void addDevice(ArrayList scannedDevices,BluetoothDevice device) + { + try + { + JSONObject obj= new JSONObject(); + obj.put("name", device.getName() ); + obj.put("string", device.toString() ); + obj.put("address", device.getAddress() ); + obj.put("alias", device.getAlias() ); + obj.put("bondState", device.getBondState() ); + obj.put("type", device.getType() ); + Log.e(TAG,"Found device " + obj.toString() ); + // If it's already paired, skip it + //if (device.getBondState() != BluetoothDevice.BOND_BONDED + scannedDevices.add(obj.toString()); + } + catch (JSONException e) + { + Log.e(TAG, "Error in device serialization." + device.toString() ); + } + sDeviceMap.put(device.getAddress(), device ); + } + static int sScanId = 0; static void scanDevices(final HaxeObject inHandler) { @@ -145,11 +168,11 @@ static void scanDevices(final HaxeObject inHandler) Log.e(TAG,"scanDevices.." + recScanId ); postDevices(inHandler, SCANNING , null ); - final ArrayList scannedDevices = new ArrayList(); sDeviceMap = new HashMap(); final BroadcastReceiver[] receiverRef = new BroadcastReceiver [1]; final Activity activity = GameActivity.getInstance().getActivity(); + final ArrayList scannedDevices = new ArrayList(); // The BroadcastReceiver that listens for discovered devices/scan done events final BroadcastReceiver receiver = new BroadcastReceiver() { @@ -184,25 +207,9 @@ else if (BluetoothDevice.ACTION_FOUND.equals(action)) { // Get the BluetoothDevice object from the Intent BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); - try - { - JSONObject obj= new JSONObject(); - obj.put("name", device.getName() ); - obj.put("string", device.toString() ); - obj.put("address", device.getAddress() ); - obj.put("alias", device.getAlias() ); - obj.put("bondState", device.getBondState() ); - obj.put("type", device.getType() ); - Log.e(TAG,"Found device " + obj.toString() ); - // If it's already paired, skip it - //if (device.getBondState() != BluetoothDevice.BOND_BONDED - scannedDevices.add(obj.toString()); - } - catch (JSONException e) - { - Log.e(TAG, "Error in device serialization." + device.toString() ); - } - sDeviceMap.put(device.getAddress(), device ); + addDevice(scannedDevices, device); + + // When discovery is finished send the devices... } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) @@ -245,25 +252,26 @@ else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) static void sendDevices(HaxeObject inHandler) { Set pairedDevices = sBluetoothAdapter.getBondedDevices(); - String [] result = null; + final ArrayList scannedDevices = new ArrayList(); // If there are paired devices Log.e(TAG,"Found paired devices " + pairedDevices.size()); if (pairedDevices.size() > 0) { - result = new String[pairedDevices.size()]; int idx = 0; // Loop through paired devices for (BluetoothDevice device : pairedDevices) { + addDevice(scannedDevices, device); // Add the name and address to an array adapter to show in a ListView - result[idx++] = device.getName();// + "\n" + device.getAddress(); - sDeviceMap.put(device.getName(), device ); + //result[idx++] = device.getName();// + "\n" + device.getAddress(); + //sDeviceMap.put(device.getAddress(), device ); } } - postDevices(inHandler, pairedDevices.size()==0 ? NO_PAIRED_DEVICES : BLUETOOTH_OK, result); + String [] devices = scannedDevices.toArray(new String[scannedDevices.size()]); + postDevices(inHandler, devices.length==0 ? NO_PAIRED_DEVICES : BLUETOOTH_OK, devices ); } static boolean getAdapter()