Bons dias a todos,
estou actualmente a desenvolver uma pequena aplicação que requeres ligação Bluetooth entre uma placa de Bitalino (módulo Bluetooth de Arduino) e um Android, sendo que tenho usado o meu (Galaxy S3) para testes.
A aplicação funciona em todos aspectos excepto no principal. Não faz a ligação com entre os dois dispositivos, no entanto, emparelha-os.
edit1:
O código completo:
public class MainActivity extends AppCompatActivity { private Switch switchOne; private ListView listView; private ToggleButton toggleButton; private ConnectThread connect; private ArrayList<String> deviceList = new ArrayList<String>(); private BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); private UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); switchOne = (Switch) findViewById(R.id.switch1); listView = (ListView) findViewById(R.id.listView); toggleButton = (ToggleButton) findViewById(R.id.toggleButton); final Intent discoverability = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); discoverability.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); final IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothDevice.ACTION_FOUND); registerReceiver(receiver, filter); toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { startActivity(discoverability); adapter.startDiscovery(); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); } else { adapter.cancelDiscovery(); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); } } }); switchOne.setOnCheckedChangeListener((new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { //startActivity(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)); adapter.enable(); } else { adapter.disable(); } } })); if (adapter.isEnabled()) { switchOne.setChecked(true); } else { switchOne.setChecked(false); } listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { adapter.cancelDiscovery(); final String info = ((TextView) arg1).getText().toString(); String address = info.substring(info.length() - 17); BluetoothDevice selected = adapter.getRemoteDevice(address); connect = new ConnectThread(selected); connect.run(); } }); } private void showToast(String text) { Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show(); } BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) { showToast("Discovery started"); deviceList = new ArrayList<String>(); toggleButton.setChecked(true); } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { toggleButton.setChecked(false); showToast("Discovery finished"); } else if (BluetoothDevice.ACTION_FOUND.equals(action)) { //Found the BlueTooth device BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); //Adds the devices to a list deviceList.add(device.getName() + "\n" + device.getAddress()); Log.i("BT", device.getName() + "\n" + device.getAddress()); listView.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, deviceList)); showToast("Found " + device.getName() + "\n" + device.getAddress()); } } }; private class ConnectThread extends Thread { private BluetoothDevice bluetoothDevice; private BluetoothSocket bluetoothSocket; public ConnectThread(BluetoothDevice device) { bluetoothDevice = device; BluetoothSocket temp = null; try { temp = device.createRfcommSocketToServiceRecord(myUUID); } catch (IOException e) { showToast("Thread: Can't create socket"); } bluetoothSocket = temp; } public void run() { adapter.cancelDiscovery(); try { bluetoothSocket.connect(); } catch (IOException e) { showToast("Thread: Can't connect socket"); try { bluetoothSocket.close(); } catch (IOException f) { showToast("Thread: Can't close socket"); } } //Manage the connection here } public void cancel() { try { bluetoothSocket.close(); } catch (IOException e) { showToast("Thread: Can't cancel"); } } } }
A toast que aparece ao carregar no item: "Thread: Can't connect socket"
Que estou a fazer mal? Que me está a faltar?
Editado por CaptMartelo, 05 September 2016 - 15:01.