无法连接到蓝牙设备。他们是 运行 时间例外

Unable to connect to a bluetooth device. Their is a run time execption

我是初学者,所以我不知道如何处理。
有人能帮帮我吗

这是我的错误信息。我不知道该如何处理。
Activity application.web.drj.MainActivity 泄露了最初在这里注册的 IntentReceiver application.web.drj.MainActivity$1@d33c9c8。您是否错过了对 unregisterReceiver() 的调用?

public class MainActivity extends ActionBarActivity {
int enablebt = 1;
ListView view1;
BluetoothAdapter adap;
List<BluetoothDevice> connected;
BluetoothSocket mmsocket;
BluetoothDevice mmdevice;
private static final UUID MY_UUID = UUID.fromString("0000110E-0000-1000-8000-00805F9B34FB");



@Override
protected void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    adap = BluetoothAdapter.getDefaultAdapter();
    connected = new ArrayList<>();
    view1 = (ListView) findViewById(R.id.list_item);


    if(adap == null)
    {
        Toast.makeText(this, "This device does not support Bluetooth.", Toast.LENGTH_SHORT).show();
    }
    if(!adap.isEnabled())
    {
        Intent enableadap = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableadap,enablebt);
    }
    final Set<BluetoothDevice> devices = adap.getBondedDevices();
    if(devices.size() > 0)
    {
        for(BluetoothDevice device : devices)
        {
            connected.add(device);
            view1.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, connected));
        }
        final BroadcastReceiver receiver = new BroadcastReceiver()
        {
            @Override
            public void onReceive(Context context, Intent intent)
            {
                String action = intent.getAction();
                if(BluetoothDevice.ACTION_FOUND.equals(action))
                {
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    connected.add(device);
                }

            }
        };
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(receiver,filter);
        Intent discoverable = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        startActivity(discoverable);
        adap.cancelDiscovery();
        view1.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id)
            {
                Thread initialize_connection = new Thread()
                {

                    @Override
                    public void run()
                    {
                        view1.setClickable(false);

                        BluetoothSocket tmp;
                        BluetoothDevice initialize_connection = (BluetoothDevice) view1.getItemAtPosition(position);
                        try
                        {
                           tmp = initialize_connection.createRfcommSocketToServiceRecord(MY_UUID);
                            mmsocket = tmp;

                        }
                        catch(Exception e)
                        {
                            Toast.makeText(getApplicationContext(),"Runtime Error",Toast.LENGTH_SHORT).show();
                        }
                        try
                        {
                            mmsocket.connect();
                            if(mmsocket.isConnected())
                            {
                                Toast.makeText(getApplicationContext(),"Connection successfull",Toast.LENGTH_SHORT).show();
                            }
                        }
                        catch(IOException e)
                        {
                            Toast.makeText(getApplicationContext(),"Connection Exception",Toast.LENGTH_SHORT).show();
                        }
                       super.run();
                    }
                };
                initialize_connection.start();

            }
        });
    }


}

由于您要在 onCreate() 中注册广播接收器,因此您必须在 onDestroy() 中调用 unregisterReceiver()

您正在 onCreate() 中注册广播接收器。

因此,您必须至少在 onDestroy() 内致电 unregisterReceiver()