I am developing the Bluetooth connection program with Android 2.1 SDK. Since devices which have not connected are also contained, this program requires time for search. Although I would like to filter and test only actually connected devices, is there any method?
And this is a general question. In my smart phone, even if it inserts sleep() in this code, ProgressDialog is not displayed. Is there any mistake in this code? public class BluetoothActivity extends Activity { private static final int REQUEST_ENABLE_BT = 2000; private BluetoothAdapter ba; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tv = (TextView)findViewById(R.id.textview); ba = BluetoothAdapter.getDefaultAdapter(); if(ba == null) { tv.setText("There is no Bluetooth adapter."); return; } if(ba.isEnabled() == false){ Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(i, REQUEST_ENABLE_BT); }else{ ConnectBluetooth(); } } private void ConnectBluetooth() { BluetoothSocket sock = ConnectBluetooth2(); if(sock == null){ AlertDialog.Builder alertDlg = new AlertDialog.Builder(this); alertDlg.setTitle("Error"); alertDlg.setMessage("Connection failed"); alertDlg.setPositiveButton( "Retry", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { ConnectBluetooth(); } }); alertDlg.setNegativeButton( "Exit", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); } }); alertDlg.create().show(); }else{ try{ InputStream in = sock.getInputStream(); OutputStream out = sock.getOutputStream(); }catch(IOException e){ Log.e("BTEST", "Transfer error", e); } } } private static final UUID MY_UUID = UUID.fromString("D6ED8549-67BD-4136-9213-0A8DE9DCB9EA"); private BluetoothSocket ConnectBluetooth2() { ProgressDialog cdlg = ProgressDialog.show(this, "Connecting", "Please wait..."); // It takes time, in order that the devices which have not connected may also search. BluetoothSocket btsock = null; if(ba.isDiscovering()) ba.cancelDiscovery(); Set<BluetoothDevice> devs = ba.getBondedDevices(); if (devs.size() > 0) { for (BluetoothDevice d : devs) { try{ btsock = d.createRfcommSocketToServiceRecord(MY_UUID); btsock.connect(); }catch(Exception e){ btsock = null; } if(btsock != null) break; } } cdlg.dismiss(); return btsock; } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { switch(requestCode){ case REQUEST_ENABLE_BT: switch(resultCode){ case Activity.RESULT_OK: ConnectBluetooth(); break; default: Toast t = Toast.makeText(this, "Connection is cancelled.", Toast.LENGTH_SHORT); t.show(); finish(); break; } } } } -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en