java - 通过蓝牙连接

标签 java android

我一直在开发 Android 蓝牙应用程序。我可以从可用设备列表中选择 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();


        }





    }

最佳答案

使用以下代码即可连接(假设您使用的是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();

关于java - 通过蓝牙连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11723586/

相关文章:

android - 平板电脑设计的谷歌开发者控制台优化技巧(minSdkVersion)

java - 将变量传递给jsp

java - 在java中求一个数字的累加和时,我们应该使用字符串还是列表来存储数字?

java - 为什么priorityQueue不对元素进行排序

java - 如何在Android Java中制作一行2个按钮和一列2个按钮?

android - 如何使用闹钟管理器设置 android 通知的标题?

android - 什么是Rage Shake API?

java - JPA实体未映射异常

android - 从库类访问主项目类

android - 是否可以检测并排放置的Android设备