示例代码“蓝牙聊天”出错

时间:2011-03-24 11:18:28

标签: android bluetooth

我试图从开发者网站运行蓝牙聊天示例,但它无法运行..我从现有源创建一个新项目,我打开但它包含很多错误..任何想法请??? http://developer.android.com/resources/samples/BluetoothChat/index.html

我用“fill_parent”替换了“match_parent”,现在控制台消息是:

[2011-03-24 13:29:17 - BluetoothChat] W/ResourceType( 5892): Bad XML block: header size 60 or total size 3932356 is larger than data size 0
[2011-03-24 13:29:17 - BluetoothChat] C:\Users\kostas\Desktop\BluetoothChat\res\menu\option_menu.xml:17: error: No resource identifier found for attribute 'showAsAction' in package 'android'
[2011-03-24 13:29:17 - BluetoothChat] C:\Users\kostas\Desktop\BluetoothChat\res\menu\option_menu.xml:21: error: No resource identifier found for attribute 'showAsAction' in package 'android'
[2011-03-24 13:29:17 - BluetoothChat] C:\Users\kostas\Desktop\BluetoothChat\res\menu\option_menu.xml:25: error: No resource identifier found for attribute 'showAsAction' in package 'android'

我删除了option_menu.xml中的行android:showAsAction="ifRoom|withText",现在控制台中没有消息.. bluetoothchat.java中出现红色错误:

private final void setStatus(int resId) {
        final **ActionBar** actionBar = **getActionBar()**;
        actionBar.setSubtitle(resId);
    }

    private final void setStatus(CharSequence subTitle) {
        final **ActionBar** actionBar = getActionBar();
        actionBar.setSubtitle(subTitle);
    }

并在bluetoothChatServise中:

 public AcceptThread(boolean secure) {
            BluetoothServerSocket tmp = null;
            mSocketType = secure ? "Secure":"Insecure";

            // Create a new listening server socket
            try {
                if (secure) {
                    tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE,
                        MY_UUID_SECURE);
                } else {
                    tmp = **mAdapter.listenUsingInsecureRfcommWithServiceRecord**(
                            NAME_INSECURE, MY_UUID_INSECURE);
                }
            } catch (IOException e) {
                Log.e(TAG, "Socket Type: " + mSocketType + "listen() failed", e);
            }
            mmServerSocket = tmp;
        }

在这里

public ConnectThread(BluetoothDevice device, boolean secure) {
    mmDevice = device;
    BluetoothSocket tmp = null;
    mSocketType = secure ? "Secure" : "Insecure";

    // Get a BluetoothSocket for a connection with the
    // given BluetoothDevice
    try {
        if (secure) {
            tmp = device.createRfcommSocketToServiceRecord(
                    MY_UUID_SECURE);
        } else {
            tmp = **device.createInsecureRfcommSocketToServiceRecord**(
                    MY_UUID_INSECURE);
        }
    } catch (IOException e) {
        Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
    }
    mmSocket = tmp;
}

(红色在** **之间)

1 个答案:

答案 0 :(得分:3)

用于创建不安全RFCOMM的API仅适用于API级别10(Android 2.3.3) ActionBar API仅适用于Android 3.0或API Level 11