@@ -19,6 +19,7 @@ public class WearableSettingsActivity extends Activity {
1919
2020 private ListView listView ;
2121 private TextView emptyView ;
22+ private WearableDeviceAdapter deviceAdapter ;
2223
2324 @ Override
2425 protected void onCreate (Bundle savedInstanceState ) {
@@ -37,7 +38,7 @@ protected void onResume() {
3738 }
3839
3940 private void refreshList () {
40- ArrayList <String > deviceList = new ArrayList <>();
41+ ArrayList <BluetoothDevice > deviceList = new ArrayList <>();
4142 BluetoothAdapter adapter = BluetoothAdapter .getDefaultAdapter ();
4243
4344 if (adapter == null || !adapter .isEnabled ()) {
@@ -46,8 +47,8 @@ private void refreshList() {
4647 }
4748
4849 Set <BluetoothDevice > bondedDevices = adapter .getBondedDevices ();
49- if (bondedDevices == null || bondedDevices . isEmpty () ) {
50- return ;
50+ if (bondedDevices != null ) {
51+ deviceList . addAll ( bondedDevices ) ;
5152 }
5253
5354 Set <String > connectedNodes = null ;
@@ -56,45 +57,58 @@ private void refreshList() {
5657 connectedNodes = service .getConnectedNodes ();
5758 }
5859
59- for (BluetoothDevice device : bondedDevices ) {
60- String status = "Disconnected" ;
61- String nodeId = device .getAddress (); // Basic mapping, improved by node DB later
62-
63- // Check by address (since WearableImpl tracks by Node ID which might be address or UUID)
64- // But getConnectedNodes returns KEYS from activeConnections.
65- // In BluetoothWearableConnection logic, we used connect.id (peer ID).
66- // However, WearableImpl checks equality against config.nodeId/peerNodeId.
67- // For now, simple check: is the address in the string set?
68- // Actually, getRemoteAddress() was used to match.
69- // Let's just list the device name and address.
70-
60+ deviceAdapter = new WearableDeviceAdapter (this , deviceList , connectedNodes );
61+ listView .setAdapter (deviceAdapter );
62+
63+ listView .setOnItemClickListener ((parent , view , position , id ) -> {
64+ BluetoothDevice device = deviceAdapter .getItem (position );
65+ Toast .makeText (this , "Connecting to " + device .getName () + "..." , Toast .LENGTH_SHORT ).show ();
66+ // Trigger connection logic if needed, currently handled by background service automatically
67+ });
68+ }
69+
70+ private static class WearableDeviceAdapter extends ArrayAdapter <BluetoothDevice > {
71+ private final Set <String > connectedNodes ;
72+
73+ public WearableDeviceAdapter (android .content .Context context , ArrayList <BluetoothDevice > devices , Set <String > connectedNodes ) {
74+ super (context , 0 , devices );
75+ this .connectedNodes = connectedNodes ;
76+ }
77+
78+ @ Override
79+ public android .view .View getView (int position , android .view .View convertView , android .view .ViewGroup parent ) {
80+ if (convertView == null ) {
81+ convertView = android .view .LayoutInflater .from (getContext ()).inflate (R .layout .wearable_device_item , parent , false );
82+ }
83+
84+ BluetoothDevice device = getItem (position );
85+ TextView nameView = convertView .findViewById (R .id .device_name );
86+ TextView addressView = convertView .findViewById (R .id .device_address );
87+ TextView statusView = convertView .findViewById (R .id .device_status );
88+ android .widget .ImageView iconView = convertView .findViewById (R .id .device_icon );
89+
90+ nameView .setText (device .getName ());
91+ addressView .setText (device .getAddress ());
92+
93+ // Simple status logic
7194 boolean isConnected = false ;
72- if (connectedNodes != null ) {
73- // Heuristic check: ConnectionThread adds by connect.id (NodeID).
74- // We don't have a map from Address -> NodeID easily here without iterating implicit structure.
75- // But wait, activeConnections keys are NodeIDs.
76- // WE DON'T KNOW the Node ID of a disconnected device easily.
77- // But for a connected device, we might see it.
78- // Let's just show "Bonded" for now, and "Connected" if we find a match?
79- // Actually, WearableImpl tracks active connections.
80- // Ideally, we'd query configurations.
81- // Let's just show device list.
82-
83- // Simple hack: If list is not empty, connection service is running.
95+ // Ideally we'd match Node ID to Address, but for now we rely on the service to potentially use address as ID
96+ // or we just show "Bonded" until we have better mapping.
97+ if (connectedNodes != null && connectedNodes .contains (device .getAddress ())) {
98+ isConnected = true ;
8499 }
85100
86- String entry = device .getName () + "\n " + device .getAddress ();
87- // If we had connection status, append it.
88- // entry += " [" + status + "]";
89-
90- deviceList .add (entry );
91- }
101+ if (isConnected ) {
102+ statusView .setText ("Connected" );
103+ statusView .setTextColor (getContext ().getResources ().getColor (android .R .color .holo_green_dark ));
104+ iconView .setAlpha (1.0f );
105+ } else {
106+ statusView .setText ("Bonded" );
107+ statusView .setTextColor (getContext ().getResources ().getColor (android .R .color .darker_gray ));
108+ iconView .setAlpha (0.6f );
109+ }
92110
93- ArrayAdapter <String > arrayAdapter = new ArrayAdapter <>(this , android .R .layout .simple_list_item_1 , deviceList );
94- listView .setAdapter (arrayAdapter );
95-
96- listView .setOnItemClickListener ((parent , view , position , id ) -> {
97- Toast .makeText (this , "Auto-connecting in background..." , Toast .LENGTH_SHORT ).show ();
98- });
111+ return convertView ;
112+ }
99113 }
100114}
0 commit comments