質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Bluetooth

Bluetoothとは短距離の間でデータを交換するための無線通信規格である。固定・モバイル両方のデバイスから、短波の電波送信を行うことで、高いセキュリティをもつパーソナルエリアネットワーク(PAN)を構築する。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

0回答

3245閲覧

Androidstudio Bluetooth通信について

rsy

総合スコア10

Bluetooth

Bluetoothとは短距離の間でデータを交換するための無線通信規格である。固定・モバイル両方のデバイスから、短波の電波送信を行うことで、高いセキュリティをもつパーソナルエリアネットワーク(PAN)を構築する。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2018/12/16 10:01

編集2018/12/16 10:08

前提・実現したいこと

AndroidStudioでAndroid同士のBluetooth通信ができるアプリを制作しています。
https://android.keicode.com/basics/bluetooth-rfcomm-socket-2.php
を参考にして制作しています。
各プログラムの一行目を自分のPC用に設定し、後はファイル名を合わせたりしていますが、
ほとんどコピー&ペーストでやっています。

サーバー側のAndroid、クライアント側のAndroid、それぞれアプリを作り、サーバー側が投げた情報をクライアント側が受け取るようにしたいのですが、Bluetooth通信がNo connectつまりつながっていないので、情報を受け取る以前の問題になってしまいます。ペアリングはしたのですが、それ以外の
原因が分かりません。

よろしくお願いいたします。

発生している問題・エラーメッセージ

特にエラーはでていません。ペアリングはしたのですが、Bluetooth通信ができていないようです。

AndroidManifest.xml

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.(名前).testcrientapplication">

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <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:configChanges="orientation"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
</manifest>

MainActivity.java

package com.example.(名前).testcrientapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

static final String TAG = "BTTEST1"; BluetoothAdapter bluetoothAdapter; TextView btStatusTextView; TextView tempTextView; BTClientThread btClientThread; final Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { String s; switch(msg.what){ case Constants.MESSAGE_BT: s = (String) msg.obj; if(s != null){ btStatusTextView.setText(s); } break; case Constants.MESSAGE_TEMP: s = (String) msg.obj; if(s != null){ tempTextView.setText(s); } break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Find Views btStatusTextView = (TextView) findViewById(R.id.btStatusTextView); tempTextView = (TextView) findViewById(R.id.tempTextView); if(savedInstanceState != null){ String temp = savedInstanceState.getString(Constants.STATE_TEMP); tempTextView.setText(temp); } // Initialize Bluetooth bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if( bluetoothAdapter == null ){ Log.d(TAG, "This device doesn't support Bluetooth."); } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString(Constants.STATE_TEMP, tempTextView.getText().toString()); } public class BTClientThread extends Thread { InputStream inputStream; OutputStream outputStrem; BluetoothSocket bluetoothSocket; public void run() { byte[] incomingBuff = new byte[64]; BluetoothDevice bluetoothDevice = null; Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices(); for(BluetoothDevice device : devices){ if(device.getName().equals(Constants.BT_DEVICE)) { bluetoothDevice = device; break; } } if(bluetoothDevice == null){ Log.d(TAG, "No device found."); return; } try { bluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord( Constants.BT_UUID); while(true) { if(Thread.interrupted()){ break; } try { bluetoothSocket.connect(); handler.obtainMessage( Constants.MESSAGE_BT, "CONNECTED " + bluetoothDevice.getName()) .sendToTarget(); inputStream = bluetoothSocket.getInputStream(); outputStrem = bluetoothSocket.getOutputStream(); while (true) { if (Thread.interrupted()) { break; } // Send Command String command = "GET:TEMP"; outputStrem.write(command.getBytes()); // Read Response int incomingBytes = inputStream.read(incomingBuff); byte[] buff = new byte[incomingBytes]; System.arraycopy(incomingBuff, 0, buff, 0, incomingBytes); String s = new String(buff, StandardCharsets.UTF_8); // Show Result to UI handler.obtainMessage( Constants.MESSAGE_TEMP, s) .sendToTarget(); // Update again in a few seconds Thread.sleep(3000); } } catch (IOException e) { // connect will throw IOException immediately // when it's disconnected. Log.d(TAG, e.getMessage()); } handler.obtainMessage( Constants.MESSAGE_BT, "DISCONNECTED") .sendToTarget(); // Re-try after 3 sec Thread.sleep(3 * 1000); } }catch (InterruptedException e){ e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if(bluetoothSocket != null){ try { bluetoothSocket.close(); } catch (IOException e) {} bluetoothSocket = null; } handler.obtainMessage( Constants.MESSAGE_BT, "DISCONNECTED - Exit BTClientThread") .sendToTarget(); } }

}

##Contents.java
package com.example.(名前).testcrientapplication;

import java.util.UUID;

public class Constants {

public static final String BT_DEVICE = "Nexus 7"; public static final UUID BT_UUID = UUID.fromString( "41eb5f39-6c3a-4067-8bb9-bad64e6e0908"); public static final String STATE_TEMP = "STATE_TEMP"; public static final int MESSAGE_BT = 0; public static final int MESSAGE_TEMP = 2;

}

##activity_main.xml

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">

<TextView android:id="@+id/btStatusTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Not connected" /> <TextView android:id="@+id/tempTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="(unknown)" android:layout_below="@id/btStatusTextView" android:textSize="24sp"/>
</RelativeLayout>

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問