Skip to content

Commit e0701c3

Browse files
committed
Added Swift sources
1 parent 069f53e commit e0701c3

25 files changed

+2047
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,6 @@ fastlane/report.xml
6565
fastlane/Preview.html
6666
fastlane/screenshots
6767
fastlane/test_output
68+
69+
# Xcode Project
70+
Android.xcodeproj

Package.resolved

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import PackageDescription
2+
3+
let package = Package(
4+
name: "Android",
5+
targets: [
6+
Target(name: "Android")
7+
],
8+
dependencies: [
9+
.Package(url: "https://github.com/SwiftJava/java_swift.git", versions: Version(2,1,1) ..< Version(3,0,0)),
10+
.Package(url: "https://github.com/SwiftJava/java_util.git", majorVersion: 2),
11+
.Package(url: "https://github.com/PureSwift/Bluetooth.git", majorVersion: 1),
12+
.Package(url: "https://github.com/PureSwift/JNI.git", majorVersion: 1)
13+
]
14+
)

Sources/Android.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// Android.swift
3+
// PureSwift
4+
//
5+
// Created by Alsey Coleman Miller on 3/21/18.
6+
//
7+
8+
import JNI
9+
10+
/// Android namespace.
11+
public enum Android: JavaPackage {
12+
13+
public static let package = ["android"]
14+
15+
public enum Content: JavaPackage {
16+
17+
public static let package = Android.package + ["content"]
18+
}
19+
20+
public enum Widget: JavaPackage {
21+
22+
public static let package = Android.package + ["widget"]
23+
}
24+
25+
public enum View: JavaPackage {
26+
27+
public static let package = Android.package + ["view"]
28+
}
29+
30+
public enum Bluetooth: JavaPackage {
31+
32+
public static let package = Android.package + ["bluetooth"]
33+
34+
public enum LE: JavaPackage {
35+
36+
public static let package = Android.Bluetooth.package + ["le"]
37+
}
38+
}
39+
}
40+
41+
public protocol JavaPackage {
42+
43+
static var package: [String] { get }
44+
}

Sources/BluetoothAdapter.swift

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
//
2+
// AndroidBluetoothAdapter.swift
3+
// PureSwift
4+
//
5+
// Created by Alsey Coleman Miller on 3/21/18.
6+
//
7+
8+
import Foundation
9+
import java_swift
10+
11+
public extension Android.Bluetooth {
12+
13+
public typealias Adapter = AndroidBluetoothAdapter
14+
}
15+
16+
public final class AndroidBluetoothAdapter: JavaObject {
17+
18+
fileprivate static let javaClassName = "android/bluetooth/BluetoothAdapter"
19+
20+
private static var JNIClass: jclass?
21+
22+
public convenience init?( casting object: java_swift.JavaObject,
23+
_ file: StaticString = #file,
24+
_ line: Int = #line ) {
25+
26+
self.init(javaObject: nil)
27+
28+
object.withJavaObject {
29+
self.javaObject = $0
30+
}
31+
}
32+
33+
public required init( javaObject: jobject? ) {
34+
super.init(javaObject: javaObject)
35+
}
36+
37+
private static var getDefaultAdapter_MethodID: jmethodID?
38+
39+
/**
40+
* Get a handle to the default local Bluetooth adapter.
41+
*
42+
* Currently Android only supports one Bluetooth adapter, but the API
43+
* could be extended to support more. This will always return the default
44+
* adapter.
45+
*
46+
* - Returns: The default local adapter, or null if Bluetooth is not supported on this hardware platform.
47+
*/
48+
@_versioned
49+
internal static func getDefaultAdapter() -> Android.Bluetooth.Adapter? {
50+
51+
var __locals = [jobject]()
52+
53+
var __args = [jvalue].init(repeating: jvalue(), count: 1)
54+
55+
let __return = JNIMethod.CallStaticObjectMethod(className: javaClassName,
56+
classCache: &JNIClass,
57+
methodName: "getDefaultAdapter",
58+
methodSig: "()L\(javaClassName);",
59+
methodCache: &getDefaultAdapter_MethodID,
60+
args: &__args,
61+
locals: &__locals)
62+
63+
defer { JNI.DeleteLocalRef( __return ) }
64+
65+
return __return != nil ? Android.Bluetooth.Adapter( javaObject: __return ) : nil
66+
}
67+
68+
/**
69+
* Get a handle to the default local Bluetooth adapter.
70+
*
71+
* Currently Android only supports one Bluetooth adapter, but the API
72+
* could be extended to support more. This will always return the default
73+
* adapter.
74+
*
75+
* - Returns: The default local adapter, or null if Bluetooth is not supported on this hardware platform.
76+
*/
77+
public static var `default`: Android.Bluetooth.Adapter? {
78+
79+
@inline(__always)
80+
get { return getDefaultAdapter() }
81+
}
82+
83+
internal func getBluetoothLeAdvertiser() -> JavaObject? {
84+
85+
// TODO
86+
return nil
87+
}
88+
89+
private static var getBluetoothLeScanner_MethodID: jmethodID?
90+
91+
/**
92+
* Returns a `BluetoothLeScanner` object for Bluetooth LE scan operations.
93+
*/
94+
@_versioned
95+
internal func getBluetoothLeScanner() -> Android.Bluetooth.LE.Scanner? {
96+
97+
var __locals = [jobject]()
98+
99+
var __args = [jvalue].init(repeating: jvalue(), count: 1)
100+
101+
let __return = JNIMethod.CallObjectMethod(object: javaObject,
102+
methodName: "getBluetoothLeScanner",
103+
methodSig: "()Landroid/bluetooth/le/BluetoothLeScanner;",
104+
methodCache: &type(of: self).getBluetoothLeScanner_MethodID,
105+
args: &__args,
106+
locals: &__locals)
107+
108+
defer { JNI.DeleteLocalRef( __return ) }
109+
110+
return __return != nil ? Android.Bluetooth.LE.Scanner( javaObject: __return ) : nil
111+
}
112+
113+
public var lowEnergyScanner: Android.Bluetooth.LE.Scanner? {
114+
115+
@inline(__always)
116+
get { return getBluetoothLeScanner() }
117+
}
118+
}

Sources/BluetoothDevice.swift

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//
2+
// AndroidBluetoothDevice.swift
3+
// PureSwift
4+
//
5+
// Created by Alsey Coleman Miller on 3/21/18.
6+
//
7+
8+
import Foundation
9+
import Bluetooth
10+
import java_swift
11+
12+
public extension Android.Bluetooth {
13+
14+
public typealias Device = AndroidBluetoothDevice
15+
}
16+
17+
public final class AndroidBluetoothDevice: JavaObject {
18+
19+
public convenience init?( casting object: java_swift.JavaObject,
20+
_ file: StaticString = #file,
21+
_ line: Int = #line ) {
22+
23+
self.init(javaObject: nil)
24+
25+
object.withJavaObject {
26+
self.javaObject = $0
27+
}
28+
}
29+
30+
public required init( javaObject: jobject? ) {
31+
super.init(javaObject: javaObject)
32+
}
33+
34+
@_versioned
35+
internal func getAddress() -> String {
36+
37+
var __locals = [jobject]()
38+
39+
var __args = [jvalue](repeating: jvalue(), count: 1)
40+
41+
let __return = JNIMethod.CallObjectMethod(object: javaObject,
42+
methodName: "getAddress",
43+
methodSig: "()Ljava/lang/String;",
44+
methodCache: &JNICache.MethodID.getAddress,
45+
args: &__args,
46+
locals: &__locals)
47+
48+
defer { JNI.DeleteLocalRef( __return ) }
49+
50+
return String(javaObject: __return)
51+
}
52+
53+
public var address: Bluetooth.Address {
54+
55+
get { return Address(rawValue: getAddress())! }
56+
}
57+
}
58+
59+
// MARK: - JNI
60+
61+
private extension Android.Bluetooth.Device {
62+
63+
/// JNI Cache
64+
struct JNICache {
65+
66+
/// JNI Java class name
67+
static let className = "android/bluetooth/BluetoothDevice"
68+
69+
/// JNI Java class
70+
static var jniClass: jclass?
71+
72+
/// JNI Method ID cache
73+
struct MethodID {
74+
75+
static var getAddress: jmethodID?
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)