zoukankan      html  css  js  c++  java
  • Bluetooth篇 开发实例之十一 官网的Bluetooth Chat sample的bug

    当没有匹配的设备和没有找到可用设备的时候。

            // If there are paired devices, add each one to the ArrayAdapter
            if (pairedDevices.size() > 0) {
                findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
                for (BluetoothDevice device : pairedDevices) {
                    mPairedDevicesArrayAdapter.add(device.getName() + "
    " + device.getAddress());
                }
            } else {
                String noDevices = getResources().getText(R.string.none_paired).toString();
                mPairedDevicesArrayAdapter.add(noDevices);
            }
                    // When discovery finds a device
                if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                    // Get the BluetoothDevice object from the Intent
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    // If it's already paired, skip it, because it's been listed already
                    if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                        mNewDevicesArrayAdapter.add(device.getName() + "
    " + device.getAddress());
                    }
                // When discovery is finished, change the Activity title
                } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                    setProgressBarIndeterminateVisibility(false);
                    setTitle(R.string.select_device);
                    if (mNewDevicesArrayAdapter.getCount() == 0) {
                        String noDevices = getResources().getText(R.string.none_found).toString();
                        mNewDevicesArrayAdapter.add(noDevices);
                    }
                }

    这边显示的两个字符串。但是,有个点击事件,把这个字符串的后17字符回传,导致出错。

       // The on-click listener for all devices in the ListViews
        private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
            public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
                // Cancel discovery because it's costly and we're about to connect
                mBtAdapter.cancelDiscovery();
    
                // Get the device MAC address, which is the last 17 chars in the View
                String info = ((TextView) v).getText().toString();
                String address = info.substring(info.length() - 17);
    
                // Create the result Intent and include the MAC address
                Intent intent = new Intent();
                intent.putExtra(EXTRA_DEVICE_ADDRESS, address);
    
                // Set result and finish this Activity
                setResult(Activity.RESULT_OK, intent);
                finish();
            }

    回到上一个aty的处理:

        private void connectDevice(Intent data, boolean secure) {
            // Get the device MAC address
            String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
            // Get the BluetoothDevice object
            BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
            // Attempt to connect to the device
            mChatService.connect(device, secure);
        }

    很明显,如果是显示"没有找到匹配的字符串" or "未找到设备" ,不是有效的MAC地址。

    解决方法:再加一层判断就好了。

  • 相关阅读:
    【HDU6609】Find the answer【线段树】
    【HDU6602】Longest Subarray【线段树+分治】
    PCIe
    NVMe Windows 支持情况
    PCIe/NVMe Soft Protocol Analyzer
    I am coming..
    hibernate自动建表技术_采用数据库反向生成技术
    struts2的执行流程
    oracle中scott用户下四个基本表SQL语句练习
    SQL语句中的having和where的区别
  • 原文地址:https://www.cnblogs.com/H-BolinBlog/p/5551539.html
Copyright © 2011-2022 走看看