0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

android java Bluetooth接続テスト用 コード

Posted at

■ テスト概要

・Android 10の端末では、socketのconnectがちゃんと機能していたが、Android 11 の端末だと精度がかなり悪くなる。

対象テストバージョン

(Bluetooth 2.0)(Bluetooth 3.0)

■ テスト用ソースコード

xml activity_main.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <Button
        android:id="@+id/connectButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="接続する" />

    <Button
        android:id="@+id/dis_connectButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="切断する" />

</LinearLayout>

java MainActivity.java

package com.example.bluetooth_con_test_01;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {

    //  プリンター アドレス :: E47FB2FDF13B

    // "00:01:95:98:D0:3B";  アダプター01
    //  00:01:95:43:98:27  アダプター 02
    private static final String DEVICE_ADDRESS = "00:01:95:98:D0:3B"; // Bluetoothアドレス
    private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); // SPPプロファイルのUUID
    private BluetoothAdapter bluetoothAdapter;
    private BluetoothSocket socket;

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

        System.out.println(" onCreate ********* APP スタート **********");

        Button connectButton = findViewById(R.id.connectButton);
        connectButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("=========== 接続ボタン:::クリック start ==============");
                connectToDevice();
            }
        });

        Button dis_connectButton = findViewById(R.id.dis_connectButton);
        dis_connectButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    if(socket != null) {
                        socket.close();
                        System.out.println(" 切断ボタン ********* 切断OK **********");
                    }
                } catch (IOException e11) {
                    e11.printStackTrace();
                }
            }
        });

        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (bluetoothAdapter == null) {
            // デバイスがBluetoothをサポートしていない場合
            showToast("デバイスBluetoothサポート外");
            finish();
        }

    }

    private void connectToDevice() {

            if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {

                BluetoothDevice device = bluetoothAdapter.getRemoteDevice(DEVICE_ADDRESS);
                try {

                   // socket = device.createRfcommSocketToServiceRecord(MY_UUID);


                    BluetoothSocket A_socket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
                    A_socket.connect();

                    System.out.println("接続 A_socket :::" + A_socket);

                    this.socket = A_socket;
                    // 接続成功時の処理
                    showToast("接続成功");
                    System.out.println("******************* 接続成功 ********************");

                } catch (IOException e) {
                    // 接続失敗時の処理
                    showToast("接続に失敗:" + e.getMessage());
                    System.out.println("接続に失敗:" + e.getMessage());


                    try {
                        if(this.socket != null) {
                            this.socket.close();
                        }
                    } catch (IOException e2) {
                        e2.printStackTrace();
                    }
                }

                System.out.println("======= if パーミッション 内 return; ::: END ======");
                return;
            }

    }


    private void showToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

}

■結果

・Android 10
(Bluetooth 2.0)(Bluetooth 3.0)
     ●      ●

・Android 11
(Bluetooth 2.0)(Bluetooth 3.0)
  ×(不安定)    ●

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?