在列表视图中单击以配对并连接到蓝牙设备

时间:2019-06-13 10:57:09

标签: android arduino android-bluetooth

我有一种项目,我想通过在Traceback (most recent call last): File "D:/github_repos/keras-rl-master/examples/dqn_cartpole.py", line 47, in <module> dqn.fit(env, nb_steps=500000, visualize=True, verbose=2) File "D:\github_repos\keras-rl-master\rl\core.py", line 224, in fit callbacks.on_episode_end(episode, episode_logs) File "D:\github_repos\keras-rl-master\rl\callbacks.py", line 67, in on_episode_end callback.on_episode_end(episode, logs=logs) File "D:\github_repos\keras-rl-master\rl\callbacks.py", line 191, in on_episode_end 'obs_mean': np.mean(self.observations[episode]), File "C:\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 2920, in mean out=out, **kwargs) File "C:\Anaconda3\lib\site-packages\numpy\core\_methods.py", line 59, in _mean arr = asanyarray(a) File "C:\Anaconda3\lib\site-packages\numpy\core\numeric.py", line 553, in asanyarray return array(a, dtype, copy=False, order=order, subok=True) MemoryError 中单击它来连接到蓝牙设备。 到目前为止,我已经制作了将附近的蓝牙设备显示到ListView中的应用程序,但是我仍然不知道如何配对和连接到该设备。

这是我application到目前为止的样子

这是我的MainActivity.java

ListView

这是我的androidmanifest.xml

package com.example.user.bluet;

import android.Manifest;
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.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

Button buttonON, buttonOFF;
BluetoothAdapter myBluetoothAdapter;


private Set<String> pairedDevices;
Button scanButton;
ListView scanListView;
ArrayList<String> stringArrayList=new ArrayList<String>();
ArrayAdapter<String> arrayAdapter;

Intent btEnablingIntent;
int requestCodeForEnable;

Set<BluetoothDevice> devicesArray;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    buttonON=(Button) findViewById(R.id.btON);
    buttonOFF=(Button) findViewById(R.id.btOFF);
    myBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
    btEnablingIntent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    requestCodeForEnable=1;

    bluetoothONMethod();
    bluetoothOFFMethod();



    scanButton=(Button) findViewById(R.id.btSCAN);
    scanListView=(ListView) findViewById(R.id.scannedListView);

    scanButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            myBluetoothAdapter.startDiscovery();

            if (!myBluetoothAdapter.isEnabled()) {
                Toast.makeText(getApplicationContext(), "Bluetooth is not enabled...", Toast.LENGTH_LONG).show();
            } else if (myBluetoothAdapter.isEnabled()) {

                Toast.makeText(getApplicationContext(), "Scanning...", Toast.LENGTH_LONG).show();
            }
        }
    });

IntentFilter intentFilter=new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(myReceiver, intentFilter);
    arrayAdapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, stringArrayList);
    scanListView.setAdapter(arrayAdapter);

//        scanListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//
//                String address = stringArrayList.get(position);
//                BluetoothDevice btDevice = myBluetoothAdapter.getRemoteDevice(address);
//
//                // TODO: Pair
//
//            }
//        });

    // Quick permission check
    int permissionCheck = this.checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION");
    permissionCheck += this.checkSelfPermission("Manifest.permission.ACCESS_COARSE_LOCATION");
    if (permissionCheck != 0) {
        this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001); //Any number
    }
}



BroadcastReceiver myReceiver=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);
                stringArrayList.add(device.getName()+ "\n" + device.getAddress());
                arrayAdapter.notifyDataSetChanged();
            }
        }
    };
private void bluetoothOFFMethod() {
    buttonOFF.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(myBluetoothAdapter.isEnabled())
            {
                myBluetoothAdapter.disable();
                Toast.makeText(getApplicationContext(), "Bluetooth is disabled", Toast.LENGTH_LONG).show();
            }
        }
    });
}

private void getPairedDevices() {
    devicesArray = myBluetoothAdapter.getBondedDevices();
    if (devicesArray.size() > 0) {
        for (BluetoothDevice device : devicesArray) {
            pairedDevices.add(device.getName()+ "\n" + device.getAddress());
        }
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode==requestCodeForEnable)
    {
        if(resultCode==RESULT_OK)
        {
            Toast.makeText(getApplicationContext(), "Bluetooth is Enabled", Toast.LENGTH_LONG).show();
        }
        else if(resultCode==RESULT_CANCELED)
        {
            Toast.makeText(getApplicationContext(),"Bluetooth enabling cancelled", Toast.LENGTH_LONG).show();
        }
    }
}


private void bluetoothONMethod() {
    buttonON.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(myBluetoothAdapter==null)
            {
                Toast.makeText(getApplicationContext(),"Bluetooth does not support on this Device", Toast.LENGTH_LONG).show();
            }
            else
            {
                if(!myBluetoothAdapter.isEnabled())
                {
                    startActivityForResult(btEnablingIntent, requestCodeForEnable);
                }
            }
        }
    });



}

<?xml version="1.0" encoding="utf-8"?>

我已经阅读了这个Stackoverflow和其他论坛中的一些线程,但是我仍然不明白。 希望你们能帮助我 感谢您的建议

0 个答案:

没有答案