我正在使用Android Studio(java)制作应用程序来扫描BLE设备,然后连接到它,最终导致我发送数据。
到目前为止,我已经创建了一个可以检测BLE设备的功能正常的扫描仪,我可以调用设备名称,MAC地址和RSSI值,但我无法连接。鉴于我有MAC地址,连接不是一小段代码吗?
我尝试过使用<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="#5B2C6F"
tools:context="org.acfmission.allchristiansfellowshipmission.MainActivity">
<ImageView
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:src="@drawable/logo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:scaleType="fitCenter"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@id/logo"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteY="182dp"
tools:layout_editor_absoluteX="0dp">
<TextView
android:id="@+id/logo_text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/logo"
app:layout_constraintTop_toTopOf="parent"
android:text="All Christians"
android:fontFamily="serif"
android:textColor="#FDFEFE"
android:textColorHighlight="#979A9A"
android:textSize="20sp" />
<TextView
android:id="@+id/logo_text_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/logo"
app:layout_constraintTop_toBottomOf="@+id/logo_text_1"
android:text="Fellowship Mission"
android:layout_below="@id/logo_text_1"
android:fontFamily="serif"
android:textColor="#FDFEFE"
android:textColorHighlight="#979A9A"
android:textSize="18sp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
tools:context="org.acfmission.allchristiansfellowshipmission.MainActivity">
<ImageView
android:contentDescription="logo"
android:src="@drawable/cathedral"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<GridLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:rowCount="7"
android:columnCount="2">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="About"
android:textSize="30sp"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="A Caring Church, committed to Global Missions and Evangelisim, Discipleship and Societal Transformation"
android:textSize="20sp" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Vision"
android:textSize="30sp"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="To Raise Christ-like Disciples, who will be agents of transformation in the wold"
android:textSize="20sp"/>
<TextView
android:text="Mission"
android:textSize="30sp"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="To bring people Jesus and MEMBERSHIP in His family, develop them to Christ-like MATURITY, equip them for their MINISTRY in the church, and life MISSION in the earth, in order to MAGNIFY God's name"
android:textSize="20sp"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Social"
android:textSize="30sp"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/facebook"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/facebook"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="acfmonline"
android:layout_toRightOf="@id/facebook"/>
<ImageView
android:id="@+id/twitter"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/twitter"
android:layout_below="@id/facebook"/>
</RelativeLayout>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Address"
android:textSize="30sp"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Address"
android:textSize="20sp"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Phone"
android:textSize="30sp"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Structure"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Structure"
android:textSize="30sp"/>
</GridLayout>
</RelativeLayout>
,它在调试部分给出了回复,但没有做任何事情。
现在我有实际扫描的MAC地址,有什么方法可以通过该地址直接连接?这也适用于我的目的,因为我只会连接到同一个BLE模块。
这是我的代码,所以你可以看到我所拥有的,所有这些都在运作
createBond()
答案 0 :(得分:0)
连接BLE外设的正确方法:
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
如果任何特性需要加密,您需要处理读/写结果“BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION”并重试。