相关疑难解决方法(0)

Android +配对设备通过蓝牙编程

我想发现bluetooth范围内的设备,列表并在点击时与它们配对.当我点击我要配对的设备名称时,我使用了以下代码,但它只关闭了应用程序.

我想知道我的代码中的错误或任何其他方式来做我需要的.

package com.marakana;

    import java.util.Set;

    import android.app.Activity;
    import android.bluetooth.BluetoothAdapter;
    import android.bluetooth.BluetoothDevice;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.Window;
    import android.view.View.OnClickListener;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.AdapterView.OnItemClickListener;


    public class BluetoothDemo extends Activity {
    // Debugging
    private static final String TAG = "DeviceListActivity";
    private static final boolean D = true;

    // Return Intent extra
    public static String EXTRA_DEVICE_ADDRESS = "device_address";

    // Member fields
    private …
Run Code Online (Sandbox Code Playgroud)

android bluetooth

45
推荐指数
3
解决办法
14万
查看次数

如何在Android上以编程方式配对蓝牙设备

对于我的应用程序,我正在尝试以编程方式配对蓝牙设备.我能够显示我想要配对的设备的配对对话框,我可以输入密码.当我按"配对"时,对话框被删除,没有任何反应.

我只需要支持Android 2.0及更新版本的设备.

目前我正在使用以下代码来启动配对进度:


public void pairDevice(BluetoothDevice device) {
        String ACTION_PAIRING_REQUEST = "android.bluetooth.device.action.PAIRING_REQUEST";
        Intent intent = new Intent(ACTION_PAIRING_REQUEST);
        String EXTRA_DEVICE = "android.bluetooth.device.extra.DEVICE";
        intent.putExtra(EXTRA_DEVICE, device);
        String EXTRA_PAIRING_VARIANT = "android.bluetooth.device.extra.PAIRING_VARIANT";
        int PAIRING_VARIANT_PIN = 0;
        intent.putExtra(EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PIN);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
Run Code Online (Sandbox Code Playgroud)

在开始配对请求之前,我停止扫描新设备.

我的应用程序具有以下蓝牙权限:

  • android.permission.BLUETOOTH_ADMIN
  • android.permission.BLUETOOTH

android bluetooth

17
推荐指数
3
解决办法
8万
查看次数

标签 统计

android ×2

bluetooth ×2