master
Zhengyu Peng 3 years ago
parent 23a154a9c3
commit b786d55c43

@ -2,6 +2,9 @@ package com.rookiedev.hexapod
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.AlertDialog import android.app.AlertDialog
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.content.Context
import android.content.DialogInterface import android.content.DialogInterface
import android.os.Bundle import android.os.Bundle
import android.os.Handler import android.os.Handler
@ -11,6 +14,7 @@ import android.widget.Button
import android.widget.ImageView import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
import com.rookiedev.hexapod.network.BluetoothService
import com.rookiedev.hexapod.network.TCPClient import com.rookiedev.hexapod.network.TCPClient
import com.rookiedev.hexapod.network.TCPClient.* import com.rookiedev.hexapod.network.TCPClient.*
import kotlinx.coroutines.* import kotlinx.coroutines.*
@ -72,6 +76,16 @@ class ControlActivity : AppCompatActivity() {
private var height = 0 private var height = 0
private var radius = 0f private var radius = 0f
private var connectInterface:String = ""
private var mContext: Context? = null
private var mac:String = ""
// private val mBluetoothAdapter: BluetoothAdapter? = null
private var bluetoothManager: BluetoothManager? = null
private var deviceAdapter: BluetoothAdapter? = null
private val mChatService: BluetoothService? = null
private var tcpClient: TCPClient? = null private var tcpClient: TCPClient? = null
private var ip: String = "" private var ip: String = ""
private var port = 0 private var port = 0
@ -97,8 +111,19 @@ class ControlActivity : AppCompatActivity() {
val myIntent = intent // gets the previously created intent val myIntent = intent // gets the previously created intent
mContext = applicationContext
bluetoothManager =
mContext!!.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
bluetoothManager
deviceAdapter = bluetoothManager!!.adapter
connectInterface = myIntent.getStringExtra("interface").toString()
if (connectInterface == "WiFi") {
ip = myIntent.getStringExtra("ip").toString() ip = myIntent.getStringExtra("ip").toString()
port = myIntent.getStringExtra("port").toString().toInt() port = myIntent.getStringExtra("port").toString().toInt()
}else if (connectInterface == "Bluetooth"){
mac = myIntent.getStringExtra("mac").toString()
}
controlWindowInsets(true) controlWindowInsets(true)
@ -341,6 +366,7 @@ class ControlActivity : AppCompatActivity() {
super.onResume() super.onResume()
progressBar.visibility = View.VISIBLE progressBar.visibility = View.VISIBLE
if (connectInterface=="WiFi") {
this.tcpClient = TCPClient(ip, port, object : OnMessageReceived { this.tcpClient = TCPClient(ip, port, object : OnMessageReceived {
override fun messageReceived(message: String?) { override fun messageReceived(message: String?) {
if (message == null) { if (message == null) {
@ -366,6 +392,9 @@ class ControlActivity : AppCompatActivity() {
} }
) )
this.tcpClient!!.start() this.tcpClient!!.start()
}else if(connectInterface=="Bluetooth"){
println("Bluetooth")
}
currentState = "standby" currentState = "standby"
controlImage!!.setImageResource(R.drawable.ic_control_circle_standby) controlImage!!.setImageResource(R.drawable.ic_control_circle_standby)

@ -17,7 +17,7 @@ import android.widget.ListView
import android.widget.TextView import android.widget.TextView
class BluetoothAdapter(mContext: Context?, private val devices: ArrayList<BluetoothDevice>) : class DeviceAdapter(mContext: Context?, private val devices: ArrayList<BluetoothDevice>) :
ArrayAdapter<BluetoothDevice?>(mContext!!, 0, devices as List<BluetoothDevice?>) { ArrayAdapter<BluetoothDevice?>(mContext!!, 0, devices as List<BluetoothDevice?>) {
@SuppressLint("MissingPermission") @SuppressLint("MissingPermission")
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
@ -86,7 +86,7 @@ class DeviceListActivity : Activity() {
val deviceList: ArrayList<BluetoothDevice> = ArrayList(pairedDevices) val deviceList: ArrayList<BluetoothDevice> = ArrayList(pairedDevices)
val bluetoothAdapter = BluetoothAdapter(this, deviceList) val bluetoothAdapter = DeviceAdapter(this, deviceList)
// Find and set up the ListView for paired devices // Find and set up the ListView for paired devices
val pairedListView: ListView = findViewById<ListView>(R.id.paired_devices) val pairedListView: ListView = findViewById<ListView>(R.id.paired_devices)

@ -1,10 +1,7 @@
package com.rookiedev.hexapod.network package com.rookiedev.hexapod.network
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.bluetooth.BluetoothAdapter import android.bluetooth.*
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothServerSocket
import android.bluetooth.BluetoothSocket
import android.content.Context import android.content.Context
import android.os.Bundle import android.os.Bundle
import android.os.Handler import android.os.Handler
@ -22,7 +19,9 @@ import java.util.*
*/ */
class BluetoothService(context: Context?, handler: Handler) { class BluetoothService(context: Context?, handler: Handler) {
// Member fields // Member fields
private val mAdapter: BluetoothAdapter private var bluetoothManager: BluetoothManager? = null
private var mContext: Context? = null
// private val mAdapter: BluetoothAdapter
private val mHandler: Handler private val mHandler: Handler
private var mSecureAcceptThread: AcceptThread? = null private var mSecureAcceptThread: AcceptThread? = null
private var mInsecureAcceptThread: AcceptThread? = null private var mInsecureAcceptThread: AcceptThread? = null
@ -316,12 +315,12 @@ class BluetoothService(context: Context?, handler: Handler) {
// Create a new listening server socket // Create a new listening server socket
try { try {
tmp = if (secure) { tmp = if (secure) {
mAdapter.listenUsingRfcommWithServiceRecord( bluetoothManager!!.adapter.listenUsingRfcommWithServiceRecord(
NAME_SECURE, NAME_SECURE,
MY_UUID_SECURE MY_UUID_SECURE
) )
} else { } else {
mAdapter.listenUsingInsecureRfcommWithServiceRecord( bluetoothManager!!.adapter.listenUsingInsecureRfcommWithServiceRecord(
NAME_INSECURE, MY_UUID_INSECURE NAME_INSECURE, MY_UUID_INSECURE
) )
} }
@ -351,7 +350,7 @@ class BluetoothService(context: Context?, handler: Handler) {
name = "ConnectThread$mSocketType" name = "ConnectThread$mSocketType"
// Always cancel discovery because it will slow down a connection // Always cancel discovery because it will slow down a connection
mAdapter.cancelDiscovery() bluetoothManager!!.adapter.cancelDiscovery()
// Make a connection to the BluetoothSocket // Make a connection to the BluetoothSocket
try { try {
@ -420,7 +419,7 @@ class BluetoothService(context: Context?, handler: Handler) {
*/ */
private inner class ConnectedThread(socket: BluetoothSocket?, socketType: String) : private inner class ConnectedThread(socket: BluetoothSocket?, socketType: String) :
Thread() { Thread() {
private val mmSocket: BluetoothSocket? private val mmSocket: BluetoothSocket? = socket
private val mmInStream: InputStream? private val mmInStream: InputStream?
private val mmOutStream: OutputStream? private val mmOutStream: OutputStream?
override fun run() { override fun run() {
@ -472,7 +471,6 @@ class BluetoothService(context: Context?, handler: Handler) {
init { init {
// Log.d(TAG, "create ConnectedThread: $socketType") // Log.d(TAG, "create ConnectedThread: $socketType")
mmSocket = socket
var tmpIn: InputStream? = null var tmpIn: InputStream? = null
var tmpOut: OutputStream? = null var tmpOut: OutputStream? = null
@ -515,7 +513,11 @@ class BluetoothService(context: Context?, handler: Handler) {
* @param handler A Handler to send messages back to the UI Activity * @param handler A Handler to send messages back to the UI Activity
*/ */
init { init {
mAdapter = BluetoothAdapter.getDefaultAdapter() mContext = context
// mAdapter = BluetoothAdapter.getDefaultAdapter()
bluetoothManager =
mContext!!.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
bluetoothManager!!.adapter
mState = STATE_NONE mState = STATE_NONE
mNewState = mState mNewState = mState
mHandler = handler mHandler = handler

Loading…
Cancel
Save