Приложение Android Studio не подключается к устройству Bluetooth - PullRequest
0 голосов
/ 12 сентября 2018

Я пытаюсь подключить свое приложение к модулю Bluetooth Arduino. Если я нажимаю на элемент в моем списке, ничего не происходит. Даже не сообщение об ошибке. Может кто-нибудь помочь? Я понятия не имею, почему ничего не происходит. Ниже мой код, я надеюсь, что это все, что вам нужно.

MainActivity.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 (Модуль: приложение):

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'
}

1 Ответ

0 голосов
/ 12 сентября 2018

Полагаю, вы говорите о списке в упражнении BT_Classic.

Ничего не происходит, потому что вы не настраиваетесь и не предпринимаете никаких действий. Примерно так:

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

    }
});
...