Android Studio App无法连接到蓝牙设备

问题描述 投票:0回答:1

我试图将我的应用程序连接到Arduino蓝牙模块。如果我点击列表中的项目,则没有任何反应。甚至没有错误消息。有人可以帮忙吗?我不知道为什么没有发生任何事情。下面是我的代码,我希望你需要它。

main activity.Java :

package com.car.bluetooth.bluetoothcar;

import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {






@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //MENÜPUNKTE
    if (id == R.id.action_settings) {
        Intent settingsintent = new Intent(MainActivity.this,
                settingsactivity.class);
                startActivity(settingsintent);
                return false;
    }
    if (id == R.id.action_connect) {
        Intent connectintent = new Intent(MainActivity.this,
                connectactivity.class);
                startActivity(connectintent);
                return false;
    }

    return super.onOptionsItemSelected(item);
}


//SeekBars

private SeekBar seekBarGas;
private TextView textViewGas;

private SeekBar seekBarSteering;
private TextView textViewSteering;

private CheckBox backwards_checkBox;
boolean rückwärts_var = false;

boolean safeMode;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();

    seekBarGas = (SeekBar) findViewById(R.id.seekBarGas);
    textViewGas = (TextView) findViewById(R.id.textViewGas);
    seekBarGas.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            textViewGas.setText(progress + "  /  " + seekBarGas.getMax());
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            seekBarGas.setProgress(0);
        }
    });

    seekBarSteering = (SeekBar) findViewById(R.id.seekBarSteering);
    textViewSteering = (TextView) findViewById(R.id.textViewSteering);
    seekBarSteering.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            textViewSteering.setText(progress + "  /  " + seekBarSteering.getMax());
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            seekBarSteering.setProgress(3);
        }
    });


    //GET DATA Settings

    Intent safeMode_Intent = getIntent();
    safeMode = safeMode_Intent.getBooleanExtra("safeMode", false);


    //Rückwärts

    CheckBox backwards_checkBox=(CheckBox)findViewById(R.id.backwards_checkBox);

    backwards_checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (((CheckBox)v).isChecked()) {
                Toast.makeText(getApplicationContext(), "Rückwärts", Toast.LENGTH_SHORT).show();
                rückwärts_var = true;

                if (safeMode == true) {
                    seekBarGas.setMax(2);
                }
            }
            else{
                Toast.makeText(getApplicationContext(), "Vorwärts", Toast.LENGTH_SHORT).show();
                rückwärts_var = false;

                if (safeMode == true){
                seekBarGas.setMax(5);
                }
        }
    }
        });


    //Bluetooth



    if (btAdapter == null){
        Toast.makeText(getApplicationContext(), "Bluetooth wird auf diesem Gerät nicht unterstützt", Toast.LENGTH_LONG).show();
    }






}





}

BT_classic.Java :

package com.car.bluetooth.bluetoothcar;

import androidx.appcompat.app.AppCompatActivity;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.content.IntentFilter;
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 android.widget.TextView;
import android.widget.Toast;

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


public class BT_Classic extends AppCompatActivity {

private Button pairedButton;
private Button discoveredButton;
private Button btonButton;
private Button btoffButton;
ListView list;

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

BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();

@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);

    list = (ListView) findViewById(R.id.list) ;



    //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());
            }

            ArrayAdapter arrayAdapter = new ArrayAdapter(BT_Classic.this, android.R.layout.simple_list_item_1, devices);
            list.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();
        }
    });










}
}

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.car.bluetooth.bluetoothcar">

<uses-feature android:name="android.hardware.bluetooth" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="Bluetooth Car"
        android:screenOrientation="landscape"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".connectactivity"
        android:label="Connect"
        android:screenOrientation="landscape" />
    <activity
        android:name=".settingsactivity"
        android:label="Settings"
        android:screenOrientation="landscape" />
    <activity
        android:name=".BT_LE"
        android:label="Connect Bluetooth Low Energy"
        android:screenOrientation="landscape"/>
    <activity
        android:name=".BT_Classic"
        android:label="Connect Bluetooth Classic"
        android:screenOrientation="landscape"
        ></activity>
</application>

build.gradle(模块:app):

apply plugin: 'com.android.application'

android {
compileSdkVersion 28
defaultConfig {
    applicationId "com.car.bluetooth.bluetoothcar"
    minSdkVersion '16'
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
implementation 'android.arch.navigation:navigation-fragment:1.0.0-alpha05'
implementation 'android.arch.navigation:navigation-ui:1.0.0-alpha05'
implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
implementation 'com.google.android.material:material:1.0.0-beta01'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
}
java android android-studio bluetooth android-bluetooth
1个回答
1
投票

我假设您正在讨论BT_Classic活动中的列表。

没有任何事情发生,因为你没有设置和行动。像这样的东西:

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
});
© www.soinside.com 2019 - 2024. All rights reserved.