Last active
January 29, 2020 14:43
-
-
Save Momijinn/25db78d148d11e0a5162 to your computer and use it in GitHub Desktop.
AndroidでBluetooth接続
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<uses-permission android:name="android.permission.BLUETOOTH"/> | |
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity{ | |
//BTの設定 | |
private BluetoothAdapter mBluetoothAdapter; //BTアダプタ | |
private BluetoothDevice mBtDevice; //BTデバイス | |
private BluetoothSocket mBtSocket; //BTソケット | |
private OutputStream mOutput; //出力ストリーム | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); | |
//端末がBluetoothに対応しているか | |
if(mBluetoothAdapter.getDefaultAdapter() == null){ | |
Toast.makeText(this,"Bluetooth対応機器ではありません",Toast.LENGTH_SHORT).show(); | |
finish(); | |
} | |
//Bluetoothが有効であるか | |
if(!mBluetoothAdapter.isEnabled()){ | |
Toast.makeText(this,"BluetoothをONにします",Toast.LENGTH_SHORT).show(); | |
mBluetoothAdapter.enable(); | |
} | |
//BluetoothのMACアドレスの定義 | |
mBtDevice = mBluetoothAdapter.getRemoteDevice("接続したいBluetoothMACアドレスを入れる"); | |
//接続が確立するまで少し待つ | |
try { | |
Thread.sleep(2000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
//OutputStreamの設定 | |
try { | |
//シリアル通信をしたいためUUIDは対応するIDを入れる | |
mBtSocket = mBtDevice.createRfcommSocketToServiceRecord( | |
UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); | |
mBtSocket.connect(); | |
mOutput = mBtSocket.getOutputStream(); | |
} catch (IOException e) { | |
Toast.makeText(getApplicationContext(),"接続できませんでした #1",Toast.LENGTH_SHORT).show(); | |
e.printStackTrace(); | |
} | |
//文字列送信 | |
try { | |
mOutput.write("TestSend".getBytes()); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment