0

私はアンドロイド用のBluetoothアプリに取り組んでいます.vaible-device-listからBtデバイスを選択できます。選択したデバイスに接続するにはどうすればよいですか? 手伝っていただけませんか?どうもありがとうございました

これが私のコードです:

public class ScanActivity extends ListActivity {


    private static final int REQUEST_BT_ENABLE = 0x1;
            public static String EXTRA_DEVICE_ADDRESS = "device_address";
            ListView listGeraete;

            BluetoothAdapter bluetoothAdapter;
            ArrayAdapter<String> arrayAdapter;

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.list);
// adapter
            bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            // list of devices
            ListView listGeraete = getListView();
            arrayAdapter = new ArrayAdapter<String>(ScanActivity.this,android.R.layout.simple_list_item_1);
            listGeraete.setAdapter(arrayAdapter);

    // if bt disable, enabling
            if (!bluetoothAdapter.isEnabled()) {
                Intent enableBt = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBt, REQUEST_BT_ENABLE);

            }


    // start discovery

            bluetoothAdapter.startDiscovery();

            registerReceiver( ScanReceiver , new IntentFilter(
                    BluetoothDevice.ACTION_FOUND)); 


        }

    private final BroadcastReceiver ScanReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {

                String action = intent.getAction();

    // find bt devices
                if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                    BluetoothDevice device = intent
                            .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    arrayAdapter.add(device.getName() + "\n" + device.getAddress());
                    arrayAdapter.notifyDataSetChanged();
                }
            }
        };


        // select a device

        public void onListItemClick(ListView l, View view, int position, long id) {


            bluetoothAdapter.cancelDiscovery();
            String devicesinfo = ((TextView) view).getText().toString();
            String address = devicesinfo.substring(devicesinfo.length());


            Intent intent = new Intent();
            intent.putExtra(EXTRA_DEVICE_ADDRESS, address);


            setResult(Activity.RESULT_OK, intent);
            Toast.makeText(getApplicationContext(),"Connecting to " + devicesinfo + 
                    address,
            Toast.LENGTH_SHORT).show();


        }





    }
4

1 に答える 1

1

次のコードを使用して接続できます (接続に RFComm を使用していると仮定します。それ以外の場合はそれに応じて変更します)

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!mBluetoothAdapter.isEnabled()) {
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, 1);
}
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
BluetoothSocket socket = (BluetoothSocket) m.invoke(device, 1);
socket.connect();
于 2012-07-30T14:39:49.463 に答える