通过蓝牙连接到Arduino

时间:2018-09-17 13:25:22

标签: java android bluetooth android-bluetooth

我正在尝试设置一个应用程序,该应用程序能够通过蓝牙模块控制Arduino。当前它说有些事情,特别是bluetoothSocket和bluetoothdevice尚未初始化,我不知道如何解决此问题,还有一些事情说

无法解析符号。

如此处所示:Screenshot

BT_Classic.java:

package com.car.bluetooth.bluetoothcar;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;



public class BT_Classic extends AppCompatActivity {

private Button pairedButton;
private Button discoveredButton;
private Button btonButton;
private Button btoffButton;
private ProgressDialog progress;
ListView listView;

private final BluetoothSocket bluetoothSocket;
private final BluetoothDevice bluetoothDevice;

private final static UUID uuid = UUID.fromString("fc5ffc49-00e3-4c8b-9cf1-6b72aad1001a");


private ArrayList<String> mDeviceList = new ArrayList<String>();

BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();


//BLUETOOTH VERBINDUNG




private static final int REQUEST_ENABLED = 0;
private static final int REQUEST_DISCOVERABLE = 0;


   public ConnectingThread(BluetoothDevice device) {

    BluetoothSocket temp = null;
    bluetoothDevice = device;

    // Get a BluetoothSocket to connect with the given BluetoothDevice
    try {
        temp = bluetoothDevice.createRfcommSocketToServiceRecord(uuid);
    } catch (IOException e) {
        e.printStackTrace();
    }
    bluetoothSocket = temp;


    }




public void run() {
    // Cancel any discovery as it will slow down the connection
    btAdapter.cancelDiscovery();

    try {
        // This will block until it succeeds in connecting to the device
        // through the bluetoothSocket or throws an exception
        bluetoothSocket.connect();
    } catch (IOException connectException) {
        connectException.printStackTrace();
        try {
            bluetoothSocket.close();
        } catch (IOException closeException) {
            closeException.printStackTrace();
        }
    }

    // Code to manage the connection in a separate thread
    /*
        manageBluetoothConnection(bluetoothSocket);
    */
}

// Cancel an open connection and terminate the thread
public void cancel() {
    try {
        bluetoothSocket.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}






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


    pairedButton = (Button) findViewById(R.id.pairedButton);
    discoveredButton = (Button) findViewById(R.id.discoveredButton);
    btonButton = (Button) findViewById(R.id.btonButton);
    btoffButton = (Button) findViewById(R.id.btoffButton);


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String  itemValue = (String) listView.getItemAtPosition(position);
            String MAC = itemValue.substring(itemValue.length() - 17);
            BluetoothDevice bluetoothDevice = btAdapter.getRemoteDevice(MAC);
            // Initiate a connection request in a separate thread
            ConnectingThread t = new ConnectingThread(bluetoothDevice);
            t.start();
        }
    });

    //Pairing Button

    pairedButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();

            ArrayList<String> devices = new ArrayList<String>();

            for (BluetoothDevice bt : pairedDevices){
                devices.add(bt.getName());
                devices.add(bt.getAddress());

            }

            ArrayAdapter arrayAdapter = new ArrayAdapter(BT_Classic.this, android.R.layout.simple_list_item_1, devices);
            listView.setAdapter(arrayAdapter);
        }
    });








    discoveredButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(!btAdapter.isDiscovering()){
                Intent bton = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                startActivityForResult(bton, REQUEST_DISCOVERABLE);
            }


        }
    });

    btonButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent bton = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(bton, REQUEST_ENABLED);
        }
    });

    btoffButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            btAdapter.disable();
        }
    });










}
}

成绩错误第1部分: Gradle Error Part 1

成绩错误第2部分: Grade Error Part 2

0 个答案:

没有答案