android 蓝牙连接失败(isSocketAllowedBySecurityPolicy start : device null)

2024-01-05

我试图用蓝牙连接两部手机(galaxy note 1、galaxy note 2),但套接字连接失败。

这是我的 LogCat:

 I/BluetoothService(24036): BEGIN mConnectThread
 D/BluetoothUtils(24036): isSocketAllowedBySecurityPolicy start : device null
 D/BluetoothService(24036): setState() 2 -> 1
 D/BluetoothService(24036): Connect Fail
 D/BluetoothService(24036): start
 V/BluetoothSocket.cpp(24036): abortNative
 V/BluetoothSocket.cpp(24036): ...asocket_abort(56) complete
 V/BluetoothSocket.cpp(24036): destroyNative
 V/BluetoothSocket.cpp(24036): ...asocket_destroy(56) complete
 D/BluetoothUtils(24036): isSocketAllowedBySecurityPolicy start : device null
 D/BluetoothService(24036): setState() 1 -> 1
 D/BluetoothService(24036): Connect Fail
 D/BluetoothService(24036): start

我不知道为什么会发生“连接失败”。

isSocketAllowedBySecurityPolicy start : device null' 有问题吗?或者蓝牙uuid不正确?

你能告诉我问题是什么以及如何解决这个问题吗?

我还添加了有关蓝牙服务部分的 src 代码

public class BluetoothService {
// Debugging
private static final String TAG = "BluetoothService";

// Intent request code
private static final int REQUEST_CONNECT_DEVICE = 1;
private static final int REQUEST_ENABLE_BT = 2;

// RFCOMM Protocol
private static final UUID MY_UUID = UUID
        .fromString("8ce255c0-200a-11e0-ac64-0800200c9a66");

private BluetoothAdapter btAdapter;

private Activity mActivity;
private Handler mHandler;

private ConnectThread mConnectThread; 
private ConnectedThread mConnectedThread; 

private int mState;

private static final int STATE_NONE = 0; // we're doing nothing
private static final int STATE_LISTEN = 1; // now listening for incoming
                                            // connections
private static final int STATE_CONNECTING = 2; // now initiating an outgoing
                                                // connection
private static final int STATE_CONNECTED = 3; // now connected to a remote
                                                // device

// Constructors
public BluetoothService(Activity ac, Handler h) {
    mActivity = ac;
    mHandler = h;

    btAdapter = BluetoothAdapter.getDefaultAdapter();
}

/**
 * Check the Bluetooth support
 * 
 * @return boolean
 */
public boolean getDeviceState() {
    Log.i(TAG, "Check the Bluetooth support");

    if (btAdapter == null) {
        Log.d(TAG, "Bluetooth is not available");

        return false;

    } else {
        Log.d(TAG, "Bluetooth is available");

        return true;
    }
}

/**
 * Check the enabled Bluetooth
 */
public void enableBluetooth() {
    Log.i(TAG, "Check the enabled Bluetooth");

    if (btAdapter.isEnabled()) {
        Log.d(TAG, "Bluetooth Enable Now");

        // Next Step
        scanDevice();
    } else {
        Log.d(TAG, "Bluetooth Enable Request");

        Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        mActivity.startActivityForResult(i, REQUEST_ENABLE_BT);
    }
}

/**
 * Available device search
 */
public void scanDevice() {
    Log.d(TAG, "Scan Device");

    Intent serverIntent = new Intent(mActivity, DeviceListActivity.class);
    mActivity.startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
}

/**
 * after scanning and get device info
 * 
 * @param data
 */
public void getDeviceInfo(Intent data) {
    // Get the device MAC address
    String address = data.getExtras().getString(
            DeviceListActivity.EXTRA_DEVICE_ADDRESS);
    // Get the BluetoothDevice object
    // BluetoothDevice device = btAdapter.getRemoteDevice(address);
    BluetoothDevice device = btAdapter.getRemoteDevice(address);

    Log.d(TAG, "Get Device Info \n" + "address : " + address);

    connect(device);
}

private synchronized void setState(int state) {
    Log.d(TAG, "setState() " + mState + " -> " + state);
    mState = state;
}

public synchronized int getState() {
    return mState;
}

public synchronized void start() {
    Log.d(TAG, "start");

    // Cancel any thread attempting to make a connection
    if (mConnectThread == null) {

    } else {
        mConnectThread.cancel();
        mConnectThread = null;
    }

    // Cancel any thread currently running a connection
    if (mConnectedThread == null) {

    } else {
        mConnectedThread.cancel();
        mConnectedThread = null;
    }
}

public synchronized void connect(BluetoothDevice device) {
    Log.d(TAG, "connect to: " + device);

    // Cancel any thread attempting to make a connection
    if (mState == STATE_CONNECTING) {
        if (mConnectThread == null) {

        } else {
            mConnectThread.cancel();
            mConnectThread = null;
        }
    }

    // Cancel any thread currently running a connection
    if (mConnectedThread == null) {

    } else {
        mConnectedThread.cancel();
        mConnectedThread = null;
    }

    // Start the thread to connect with the given device
    mConnectThread = new ConnectThread(device);

    mConnectThread.start();
    setState(STATE_CONNECTING);
}

// ConnectedThread 
public synchronized void connected(BluetoothSocket socket,
        BluetoothDevice device) {
    Log.d(TAG, "connected");

    // Cancel the thread that completed the connection
    if (mConnectThread == null) {

    } else {
        mConnectThread.cancel();
        mConnectThread = null;
    }

    // Cancel any thread currently running a connection
    if (mConnectedThread == null) {

    } else {
        mConnectedThread.cancel();
        mConnectedThread = null;
    }

    // Start the thread to manage the connection and perform transmissions
    mConnectedThread = new ConnectedThread(socket);
    mConnectedThread.start();

    setState(STATE_CONNECTED);
}

//  thread stop
public synchronized void stop() {
    Log.d(TAG, "stop");

    if (mConnectThread != null) {
        mConnectThread.cancel();
        mConnectThread = null;
    }

    if (mConnectedThread != null) {
        mConnectedThread.cancel();
        mConnectedThread = null;
    }

    setState(STATE_NONE);
}

public void write(byte[] out) { // Create temporary object
    ConnectedThread r; // Synchronize a copy of the ConnectedThread
    synchronized (this) {
        if (mState != STATE_CONNECTED)
            return;
        r = mConnectedThread;
    } // Perform the write unsynchronized r.write(out); }
}

private void connectionFailed() {
    setState(STATE_LISTEN);
}

private void connectionLost() {
    setState(STATE_LISTEN);

}

private class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;

    public ConnectThread(BluetoothDevice device) {
        mmDevice = device;
        BluetoothSocket tmp = null;

        try {
            tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) {
            Log.e(TAG, "create() failed", e);
        }
        mmSocket = tmp;
    }

    public void run() {
        Log.i(TAG, "BEGIN mConnectThread");
        setName("ConnectThread");

        btAdapter.cancelDiscovery();

        try {
            mmSocket.connect();
            Log.d(TAG, "Connect Success");

        } catch (IOException e) {
            connectionFailed(); 
            Log.d(TAG, "Connect Fail");

            try {
                mmSocket.close();
            } catch (IOException e2) {
                Log.e(TAG,
                        "unable to close() socket during connection failure",
                        e2);
            }
            BluetoothService.this.start();
            return;
        }

        synchronized (BluetoothService.this) {
            mConnectThread = null;
        }

        connected(mmSocket, mmDevice);
    }

    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
            Log.e(TAG, "close() of connect socket failed", e);
        }
    }
}

private class ConnectedThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;

    public ConnectedThread(BluetoothSocket socket) {
        Log.d(TAG, "create ConnectedThread");
        mmSocket = socket;
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) {
            Log.e(TAG, "temp sockets not created", e);
        }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    public void run() {
        Log.i(TAG, "BEGIN mConnectedThread");
        byte[] buffer = new byte[1024];
        int bytes;

        // Keep listening to the InputStream while connected
        while (true) {
            try {
                bytes = mmInStream.read(buffer);

            } catch (IOException e) {
                Log.e(TAG, "disconnected", e);
                connectionLost();
                break;
            }
        }
    }

    /**
     * Write to the connected OutStream.
     * 
     * @param buffer
     *            The bytes to write
     */
    public void write(byte[] buffer) {
        try {
            mmOutStream.write(buffer);

        } catch (IOException e) {
            Log.e(TAG, "Exception during write", e);
        }
    }

    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
            Log.e(TAG, "close() of connect socket failed", e);
        }
    }
}

}

connect fail发生在 connectThread 处。


尝试通过以下方式获取正确的 UUID:

BluetoothDevice device;
ParcelUuid list[] = device.getUuids();
//use e.g. first list[0] 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

android 蓝牙连接失败(isSocketAllowedBySecurityPolicy start : device null) 的相关文章

  • 如何使用 ProGuard 将所有方法保留在类中

    我使用 ProGuard 来优化我的 Android 应用程序 然而 对于 Android 仪器测试 我需要一些 但不是全部 类来保留所有成员 我尝试了各种方法 最后一个是 keepclassmembers public class com
  • 如何在 Android / Java 中获取图像的分辨率

    如何在 Android 中找到任何图像的分辨率 获取存储在磁盘中的图像大小的有效方法 例如获取用户选择上传的图像文件的大小 是使用BitmapFactory Options并设置inJustDecodeBounds为真 这样做你就会获取图像
  • TypedArray 的 getResourceId 方法

    我正在阅读有关的文档获取资源Id https developer android com reference android content res TypedArray html getResourceId int 20int 方法 它说
  • 与其他图标相比,AppCompat ShareActionProvider 图标太大

    我将 ActionBarSherlock 更改为 AppCompat v7 我已经完成了使其工作所需的所有更改 但是共享图标 使用 ShareActionProvider 发生了一些奇怪的情况 与其他图标相比 共享图标太大 我还使用支持库进
  • 强制用户在 Android 中的 EditText 中输入内容

    我的活动中有几个编辑文本 我希望我的用户在提交表单之前正确输入 我该怎么做 我还有旋转器和 RadioGroup 按钮 你可以加验证在提交按钮上单击 private boolean validateFields int yourDesire
  • 安卓无法玩ogg

    有人知道这是什么意思吗 ogg使用phonegap is Media播放 它使用MediaPlayer 05 26 15 41 50 007 1160 3631 E AudioFlinger no more track names avai
  • GridView 中多次调用 getView()

    我的 Activity 由包含 40 多个元素的 GridView 组成 开始活动后 用户最多可以看到 15 个项目 3 行 每行 5 个项目 我在 getView 正文中编写传递给获取 View 的 LogCat 编号 Log i get
  • 在 Anko DSL 中创建自定义 View/ViewGroup 类

    我想创建一个自定义视图 它只是一些 Android 视图的包装 我考虑创建一个自定义 ViewGroup 来管理其子视图的布局 但我不需要这么复杂 我基本上想做的是 class MainActivity verticalLayout tex
  • android webview 函数 onPagefinished 被调用两次

    我的android webview功能onPageFinished被调用两次 我不知道为什么 但它在 android 2 2 上运行良好 但当我将其升级到 4 时 它就不起作用了 代码附在下面 Code Override public vo
  • 使用 RecyclerView 适配器在运行时更改布局屏幕

    我有两个布局文件 如下所示 如果列表中存在数据 则我显示此布局 当列表为空时 我会显示此布局 现在我想在运行时更改布局 当用户从列表中删除最后一项时 我想将布局更改为第二张图片中显示的 空购物车布局 In getItemCount Recy
  • Android 应用被 Google Play 拒绝

    我最近向 Google Play 商店提交了一个 Android 应用程序 但收到一条消息说我的应用程序已被拒绝 我不确定问题是什么 也找不到确切的解决方案 拒绝原因 违反了禁止行为条款 内容政策 经过定期审核后 我们确定您的应用程序支持
  • 如何防止布局的方向改变,而不是整个屏幕/活动的方向改变

    我需要一个子布局 可以是任何布局 例如FrameLayout or RelativeLayout 忽略方向变化并始终保持横向 但不是它的父级或任何其他兄弟布局 视图 它们应该相应地改变它们的方向 因此 我不能使用setRequestedOr
  • 导航抽屉默认片段

    我是一名新手开发人员 我正在将导航抽屉与 android support v7 集成到我的应用程序中 我有一个问题 当我启动应用程序时 主要布局是这样的
  • 如何在 Android NDK 中创建新的 NativeWindow 而无需 Android 操作系统源代码?

    我想编译一个 Android OpenGL 控制台应用程序 您可以直接从控制台启动 Android x86 运行 或者从 Android x86 GUI 内的 Android 终端应用程序运行 这个帖子 如何在 Android NDK 中创
  • Android Studio - 值必须 ≥ 0

    我在 Android Studio 中收到与光标有关的错误 我的代码中有以下行 String data cursor getString cursor getColumnIndex columnIndex columnIndex 被传递到该
  • onClick 未在带有子项的 LinearLayout 上触发

    我有一个自定义的 LinearLayout 和一个较小的 TextView 子级 我希望能够单击 TextView 未覆盖的区域 因此我将 clickable true 和 onclicklistener 设置为 LinearLayout
  • 将图像添加到自定义 AlertDialog

    我制作了一个 AlertDialog 让用户可以从我显示的 4 个选项中选择一个 前 3 个让他们在单击号码时直接拨打号码 第 4 个显示不同的视图 现在看起来是这样的 由于第四个选项的目的是不同的任务 我想让它看起来不同 因为用户可能会感
  • 直接使用从密钥库加载的 SecretKey 时,密钥用户未经过身份验证

    我正在尝试使用 Cipher 和在 KeyStore 中加载的 SecretKey 来加密数据 但总是收到此错误 导致 android security KeyStoreException 关键用户未经过身份验证 我尝试自己创建 Secre
  • FCM onMessageReceived 应用程序运行时返回空白消息和标题

    正如您在标题中所写 当应用程序关闭时 它运行良好 并且onMessageReceived获取消息正文和标题 但如果应用程序处于前台模式 运行模式 则可以发送通知 但没有消息和标题 请问该怎么办 代码 Override public void
  • 安卓的限制

    我需要构建一个应用程序 该应用程序拍摄相机图像并将其上传到网络 在网络上进行一些处理并返回真 假 我在这方面遇到了一些问题 希望得到澄清 1 我的应用程序有什么方法可以知道 Android 相机捕获的图像吗 我从这里明白了什么 Androi

随机推荐